package com.opd.soeghtool;

import android.Manifest;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import android.view.DisplayCutout;
import android.view.View;
import android.view.WindowInsets;
import android.widget.Toast;

import com.nostra13.universalimageloader.BuildConfig;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;

public class GlobalVar extends Application {

    private final String TAG = "SOEGV";
    private final String LOCAL_PATH = "/SOEGHTool";
    private int mCurPhone, mCurGame;
    private byte u8IsPtExist[];
    private int[] ptTbl = {
            // A
           // 29501, 29840,    //卧倒
            32767,32767,
// B
            29501, 18926,    //右枪
// X
            29603, 13672,    //左枪
// Y
            22831, 30392,    //跳跃
// Up
            1231, 30871,    //关闭弹出窗口
// Down
            359, 31207,    //地图
// Left
            12057, 4845,    //左探头
// Right
            12108, 7292,    //右探头
// L1
            17033, 30392,    //开镜
// R1
            30065, 27225,    //蹲下
// L2
            30681, 24923,    //换子弹
// R2
            24575, 27465,    //右攻击
// Back
            30681, 21324,    //扔手雷
// Turbo
            8619, 27033,    //跑
// Start
            30681, 11178,    //急救
// Select
            29911, 3070,    //背包
// Left Rocker Center
            24524, 6428,    //左摇杆
// Right Rocker Center
            11749, 18206,    //右摇杆
// Left Rocker Center
            24524, 6428,    //左摇杆
// Right Rocker Center
            11749, 18206,    //右摇杆
    };

    int[] coorTbl;
    int[][] attrTbl;
    int[] attrEvTbl;
    byte[] kbTbl;
    int[][] combo_ptTbl;
    int[] localSetting_KB;
    int[] localSetting_GP;
    private final int INTERFACE_CLASS = 0x03;//30264;
    private final int INTERFACE_SUBCLASS = 0x00;//30264;
    private final int INTERFACE_PROTOCOL = 0x00;//30264;

    private final int USBCMD_TIMEOUT = 20000;
    private final int USBCMD_TIMEOUT_LONG = 100000;
    private boolean bFloatMode, bIsWaitForUpdate;
    String strFwVer, strBuildDate;
    private int mScreenWidth = 1080;
    private int mScreenHeight = 1920;

    private String []packageName;
    private int mGameListTotalNum;
    private int mGameDefExist;
    private int mGameGpTotalNum;
    private int mUserGameNum;

    // MP1 BX1
    private String []packageNameKB;
    private String []strUserGameNameKB;
    private int mGameListTotalNumKB;
    private int mGameDefExistKB;
    private int mGameGpTotalNumKB;
    private int mUserGameNumKB;

    private int mLastConnectDeviceIndex = 0;

    UsbManager myUsbManager;
    UsbDevice myUsbDevice;
    UsbInterface myInterface;
    UsbEndpoint epOut;
    UsbEndpoint epIn;
    UsbDeviceConnection DeviceConnection;

    private boolean bIsUsbConnect = false;
    private boolean bIsTransferMode = false;


    private int m_uCmdIndex = 18;
    private int m_usCheckSum = 0;
    private int m_u32CodeVer = 0x01000100;
    private boolean bResendFlag;

    private boolean bForceS2Enable =  false;


    byte[] DataBuf; // for usb
    // for user add game
    char [][]cUserEditGame;
    String strDeviceName;


    int menuOnPos_x=0,menuOnPos_y=0;

    boolean bIsPortraitGame = false,bIsAutoSwitchToHDMI = true,bIsManualSetToHDMI = false,bIsForceViewOut = false,bIsVirtualMouse=true;
    private boolean bIsAddToList = false,bIsSocketRecvServiceRunning=false;
    private boolean bIsLogOn = false,bIsSimulateMode =  false,bIsPC360Mode =  false,bIsInjectMode=true;
    private boolean bIsGameServiceRunning = false, bIsSettingServiceRunning = false,bIsFloatServiceRunning =false;
    private boolean bIsAudioOut = false;
    String curPkgName="",curLabelName="";

    int mPt_Tbl_View_Size = 2, mPt_Tbl_View_Alpha = 7;
    int mFloatLogoSize = 5,mNormalIconSize=5,mNormalIconAlpha=5;

    //KB OUTPUT
    int mManualSetHDMIWidth = 1920;
    int mManualSetHDMIHeight = 1080;
    int mRockerMoveRange_KB = 100;
    int mVirtualMouseInterval = 20;
    int mSwitchToMouseKey_kb = 0x129;

    long mAppSize = 0;
    // User Guide
    // Advance user guide
    boolean bIsEnterAdvGuide = false, bIsEnterGameGuide = false, bIsNotchFlag = false;

    boolean bIsAdvSetNeedGuide = true, bIsEnterGameNeedGuide = true;

    boolean bIsFloatMenuGuide =  true,bIsSupportMenuKey = false,bIsSupportMenuKeyOnOff = false;

    int mCurLanguageIndex = 0;

    final int SUPPORT_MENU_KEY_VER =    ((1<<24)+(1<<8)+13);

    boolean bIsInjectData = false,bIsInjectServerRunning = false;

    static public int    TI_LEFT_ROCKER_BUTTON=16;
    static public int    TI_RIGHT_ROCKER_BUTTON=17;
    static public int    TI_LEFT_ROCKER=18;
    static public int    TI_RIGHT_ROCKER=19;
    static public int    TI_BUTTON_MAX=20;

    static public int    DEVICE_REPORT_MAX_WIDTH   = 0x7FFF;
    static public int    DEVICE_REPORT_MAX_HEIGHT   = 0x7FFF;

    @Override
    public void onCreate() {
        super.onCreate();
        mCurPhone = 0;
        mCurGame = 0;
        bFloatMode = false;
        bIsWaitForUpdate = false;
        strFwVer = "";
        strBuildDate = "";

        mGameListTotalNum = 0;
        mGameGpTotalNum = 10;
        DataBuf = new byte[Transfer_Con.HID_PACKET_SIZE];
        mUserGameNum = 0;
        mGameDefExist = 0;
        // MP1 BX1
        mGameListTotalNumKB =0;
        mGameDefExistKB=0;
        mGameGpTotalNumKB=0;
        mUserGameNumKB=0;
        if(Transfer_Con.ENABLE_GAME_DATA_IN_PHONE_GP)
        {
            packageName = new String[Transfer_Con.MAX_GAME_NUM_IN_PHONE];
            packageNameKB = new String[Transfer_Con.MAX_GAME_NUM_IN_PHONE];
            strUserGameNameKB = new String[Transfer_Con.MAX_GAME_NUM_IN_PHONE];
            u8IsPtExist = new byte[Transfer_Con.MAX_GAME_NUM_IN_PHONE];
        }
        else {
            packageName = new String[Transfer_Con.MAX_GAME_NUM];
            packageNameKB = new String[Transfer_Con.MAX_GAME_NUM];
            strUserGameNameKB = new String[Transfer_Con.MAX_GAME_NUM];
            u8IsPtExist = new byte[Transfer_Con.MAX_GAME_NUM];
        }
        cUserEditGame = new char[100][20];

        strDeviceName = "";
        coorTbl = new int[Transfer_Con.MAX_COORDINATE*2];
        attrTbl = new int[Transfer_Con.MAX_COORDINATE][3];
        attrEvTbl =  new int[1024]; //

        //Log.d(TAG,String.format("-------------------->> %d  ,,,,%d",coorTbl.length,Transfer_Con.MAX_COORDINATE*2));
        localSetting_KB = new int[100];
        localSetting_GP = new int[100];
        kbTbl = new byte[5];
        for (int a = 0; a < 5; a++) {
            if(mCurLanguageIndex==2)
                cUserEditGame[a] = ("自定义游戏" + String.valueOf(a + 1)).toCharArray();
            else
                cUserEditGame[a] = ("User Game" + String.valueOf(a + 1)).toCharArray();
        }
        combo_ptTbl = new int[4][TI_BUTTON_MAX*2];
        // ptTbl = new int[mGlobalVar.getDeviceMaxButton()*2];
        if(BuildConfig.DEBUG)
            bIsLogOn = true;
    }

    public void setScreenWidth(int width)
    {
        mScreenWidth = width;
    }
    public int getScreenWidth()
    {
        return mScreenWidth;
    }
    public void setScreenHeight(int height)
    {
        mScreenHeight = height;
    }
    public int getScreenHeight()
    {
        return mScreenHeight;
    }
    public boolean getIsGameServiceRunning() {
        return bIsGameServiceRunning;
    }

    public void setIsGameServiceRunning(boolean val) {
        bIsGameServiceRunning = val;
    }

    public boolean getIsSettingServiceRunning() {
        return bIsSettingServiceRunning;
    }

    public void setIsSettingServiceRunning(boolean val) {
        bIsSettingServiceRunning = val;
    }

    public boolean getIsFloatServiceRunning() {
        return bIsFloatServiceRunning;
    }

    public void setIsFloatServiceRunning(boolean val) {
        bIsFloatServiceRunning = val;
    }

    public boolean getIsSocketRecvServiceRunning() {
        return bIsSocketRecvServiceRunning;
    }
    public void setIsSocketRecvServiceRunning(boolean val) {
        bIsSocketRecvServiceRunning = val;
    }



    public boolean getIsSupportMenuKey(){return bIsSupportMenuKey;}
    public void setIsSupportMenuKey(boolean val) {
        bIsSupportMenuKey = val;
    }

    public boolean getIsSupportMenuKeyOnOff(){return bIsSupportMenuKeyOnOff;}
    public void setIsSupportMenuKeyOnOff(boolean val) {
        bIsSupportMenuKeyOnOff = val;
    }


    public boolean getIsForceS2Enable() {return bForceS2Enable;}
    public void setIsForceS2Enable(boolean val) {bForceS2Enable = val;}

    public int getCurLanguageIndex(){return mCurLanguageIndex;}
    public void setCurLanguageIndex(int val){mCurLanguageIndex = val;}

    public int getLastConnectDeviceIndex(){return mLastConnectDeviceIndex;}
    public void setLastConnectDeviceIndex(int val){mLastConnectDeviceIndex = val;}

    public boolean getIsDeviceSameType()
    {
        if((getDeviceIndex()==Transfer_Con.DEVICE_HN1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_GM1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_HN2_INDEX))
        {
                return (mLastConnectDeviceIndex==Transfer_Con.DEVICE_HN1_INDEX)||(mLastConnectDeviceIndex==Transfer_Con.DEVICE_GM1_INDEX)||(mLastConnectDeviceIndex==Transfer_Con.DEVICE_HN2_INDEX);
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_MP1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_BX1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_BX2_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_MR1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_U41_INDEX))
        {
            return (mLastConnectDeviceIndex==Transfer_Con.DEVICE_MP1_INDEX)||(mLastConnectDeviceIndex==Transfer_Con.DEVICE_BX1_INDEX)||(mLastConnectDeviceIndex==Transfer_Con.DEVICE_BX2_INDEX)||(mLastConnectDeviceIndex==Transfer_Con.DEVICE_MR1_INDEX)||(mLastConnectDeviceIndex==Transfer_Con.DEVICE_U41_INDEX);
        }
        return false;
    }


    public int getmCurPhone() {
        return mCurPhone;
    }

    public void setmCurPhone(int val) {
        mCurPhone = val;
    }

    public int getmCurGame() {
        return mCurGame;
    }

    public void setmCurGame(int val) {
        mCurGame = val;
    }

    public int[] getPTTable() {
        return ptTbl;
    }

    public void setPtTbl(int[] val) {
        ptTbl = val;
    }

    public int[] getComboPTTable(byte index) {
        return combo_ptTbl[index];
    }

    public void setComboPtTbl(byte index, int[] val) {
        combo_ptTbl[index] = val;
    }


    public boolean getIsPtExit(int phone, int game) {
        return (((u8IsPtExist[game]) & (1 << phone)) == (1 << phone));
    }

    public void setIsPtExit(int phone, int game, boolean val) {
        if (val)
            u8IsPtExist[game] |= (1 << phone);
        else
            u8IsPtExist[game] &= (~(1 << phone));
    }

    public boolean getIsAddToList() {
        return bIsAddToList;
    }

    public void setIsAddToList(boolean val) {
        bIsAddToList = val;
    }

    public boolean getFloatMode() {
        return bFloatMode;
    }

    public void setFloatMode(boolean val) {
        bFloatMode = val;
    }

    public boolean getIsWaitForUPdate() {
        return bIsWaitForUpdate;
    }

    public void setIsWaitForUPdate(boolean val) {
        bIsWaitForUpdate = val;
    }

    public boolean getIsLogOn() {
        return bIsLogOn;
    }

    public void setIsLogOn(boolean val) {
        bIsLogOn = val;
    }

    public boolean getIsFloatMenuGuide()
    {
        return bIsFloatMenuGuide;
    }
    public void setIsFloatMenuGuide(boolean val)
    {
        bIsFloatMenuGuide = val;
    }

    public boolean getIsInjectData(){ return bIsInjectData; };
    public void setIsInjectData(boolean val){ bIsInjectData = val; }
    public boolean getIsInjectServerRunning(){ return bIsInjectServerRunning; };
    public void setIsInjectServerRunning(boolean val){ bIsInjectServerRunning = val; }

    public boolean getIsSimulateMode(){ return bIsSimulateMode; };
    public void setIsSimulateMode(boolean val){ bIsSimulateMode = val; }

    public boolean getIsPC360Mode(){ return bIsPC360Mode; };
    public void setIsPC360Mode(boolean val){ bIsPC360Mode = val; }



    public boolean getIsInjectMode(){ return bIsInjectMode; };
    public void setIsInjectMode(boolean val){ bIsInjectMode = val; }

    public int getDeviceIndex() {
        if(bIsUsbConnect) {
            if (strDeviceName.contains(Transfer_Con.SOE_HN2))
                return Transfer_Con.DEVICE_HN2_INDEX;
            else if (strDeviceName.contains(Transfer_Con.SOE_HN1))
                return Transfer_Con.DEVICE_HN1_INDEX;
            if (strDeviceName.contains(Transfer_Con.SOE_GM2) || strDeviceName.contains("GN2"))
                return Transfer_Con.DEVICE_GM2_INDEX;
            else if (strDeviceName.contains(Transfer_Con.SOE_GM1) || strDeviceName.contains("GN1"))
                return Transfer_Con.DEVICE_GM1_INDEX;
            else if (strDeviceName.contains(Transfer_Con.SOE_MP1))
                return Transfer_Con.DEVICE_MP1_INDEX;
            else if (strDeviceName.contains(Transfer_Con.SOE_BX1))
                return Transfer_Con.DEVICE_BX1_INDEX;
            else if (strDeviceName.contains(Transfer_Con.SOE_BX2))
                return Transfer_Con.DEVICE_BX2_INDEX;
            else if (strDeviceName.contains(Transfer_Con.SOE_MR1))
                return Transfer_Con.DEVICE_MR1_INDEX;
            else if (strDeviceName.contains(Transfer_Con.SOE_U41))
                return Transfer_Con.DEVICE_U41_INDEX;
            else
                return Transfer_Con.DEVICE_UNKNOWN_INDEX;
        }
        else
        {
            return Transfer_Con.DEVICE_UNKNOWN_INDEX;
        }
    }

    public String getDeviceName() {
        if(bIsUsbConnect) {
            if (strDeviceName.contains(Transfer_Con.SOE_HN2))
                return "N1 Pro ";
            else if (strDeviceName.contains(Transfer_Con.SOE_GM2) || strDeviceName.contains("GN2"))
                return "M2 ";
            else if (strDeviceName.contains(Transfer_Con.SOE_GM1) || strDeviceName.contains("GN1"))
                return "M1 ";
            else if (strDeviceName.contains(Transfer_Con.SOE_MP1))
                return "GSV56FG ";
            else if (strDeviceName.contains(Transfer_Con.SOE_BX1))
                return "X1 ";
            else if (strDeviceName.contains(Transfer_Con.SOE_BX2))
                return "X2 ";
            else if (strDeviceName.contains(Transfer_Con.SOE_MR1))
                return "GSV56FG+ ";
            else if (strDeviceName.contains(Transfer_Con.SOE_U41))
                return "U41 ";
            else
                return "N1 ";
        }
        else
        {
            return "NA ";
        }
    }

    public void setDeviceName(String str) {
        Log.d(TAG,"Name: "+str);
        strDeviceName = str;
    }

    public void InitDeviceButtonIndex()
    {
        if(strDeviceName.contains(Transfer_Con.SOE_GM1)||strDeviceName.contains(Transfer_Con.SOE_GM2)) {
            TI_LEFT_ROCKER_BUTTON=16;
            TI_RIGHT_ROCKER_BUTTON = TI_LEFT_ROCKER_BUTTON + 1;
            TI_LEFT_ROCKER=TI_RIGHT_ROCKER_BUTTON + 1;
            TI_RIGHT_ROCKER=TI_LEFT_ROCKER + 1;
            TI_BUTTON_MAX=TI_RIGHT_ROCKER + 1;

            DEVICE_REPORT_MAX_WIDTH = 0x7FFF;
            DEVICE_REPORT_MAX_HEIGHT = 0x7FFF;

            Transfer_Con.tButtonName[0] ="B";
            Transfer_Con.tButtonName[1] ="A";
            Transfer_Con.tButtonName[2] ="Y";
            Transfer_Con.tButtonName[3] ="X";
            Transfer_Con.tButtonName[4] ="UP";
            Transfer_Con.tButtonName[5] ="DOWN";
            Transfer_Con.tButtonName[6] ="LEFT";
            Transfer_Con.tButtonName[7] ="RIGHT";
            Transfer_Con.tButtonName[8] ="L1";
            Transfer_Con.tButtonName[9] ="R1";
            Transfer_Con.tButtonName[10] ="L2";
            Transfer_Con.tButtonName[11] ="R2";
            Transfer_Con.tButtonName[12] ="SELECT";
            Transfer_Con.tButtonName[13] ="CLEAR";
            Transfer_Con.tButtonName[14] ="START";
            Transfer_Con.tButtonName[15] ="TURBO";
            Transfer_Con.tButtonName[16] ="L3";
            Transfer_Con.tButtonName[17] ="R3";
            Transfer_Con.tButtonName[18] ="L-S";
            Transfer_Con.tButtonName[19] ="R-S";
        }
        else if(strDeviceName.contains(Transfer_Con.SOE_HN1)||strDeviceName.contains(Transfer_Con.SOE_HN2))
        {
            TI_LEFT_ROCKER=16;
            TI_RIGHT_ROCKER = TI_LEFT_ROCKER + 1;
            TI_BUTTON_MAX=TI_RIGHT_ROCKER + 1;
            TI_LEFT_ROCKER_BUTTON=TI_BUTTON_MAX + 1;
            TI_RIGHT_ROCKER_BUTTON=TI_LEFT_ROCKER_BUTTON + 1;
            DEVICE_REPORT_MAX_WIDTH = 0x7FFF;
            DEVICE_REPORT_MAX_HEIGHT = 0x7FFF;
            Transfer_Con.tButtonName[0] ="A";
            Transfer_Con.tButtonName[1] ="B";
            Transfer_Con.tButtonName[2] ="X";
            Transfer_Con.tButtonName[3] ="Y";
            Transfer_Con.tButtonName[4] ="UP";
            Transfer_Con.tButtonName[5] ="DOWN";
            Transfer_Con.tButtonName[6] ="LEFT";
            Transfer_Con.tButtonName[7] ="RIGHT";
            Transfer_Con.tButtonName[8] ="L1";
            Transfer_Con.tButtonName[9] ="R1";
            Transfer_Con.tButtonName[10] ="L2";
            Transfer_Con.tButtonName[11] ="R2";
            Transfer_Con.tButtonName[12] ="S2";
            Transfer_Con.tButtonName[13] ="S1";
            Transfer_Con.tButtonName[14] ="S4";
            Transfer_Con.tButtonName[15] ="S3";
            Transfer_Con.tButtonName[16] ="L-S";
            Transfer_Con.tButtonName[17] ="R-S";
            Transfer_Con.tButtonName[18] ="L-S";
            Transfer_Con.tButtonName[19] ="R-S";
        }
        else if(strDeviceName.contains(Transfer_Con.SOE_MP1))
        {
            TI_LEFT_ROCKER=16;
            TI_RIGHT_ROCKER = TI_LEFT_ROCKER + 1;
            TI_BUTTON_MAX=TI_RIGHT_ROCKER + 1;
            TI_LEFT_ROCKER_BUTTON=TI_BUTTON_MAX + 1;
            TI_RIGHT_ROCKER_BUTTON=TI_LEFT_ROCKER_BUTTON + 1;
            DEVICE_REPORT_MAX_WIDTH = 0x7FFF;
            DEVICE_REPORT_MAX_HEIGHT = 0x7FFF;
            Transfer_Con.tButtonName[0] ="A";
            Transfer_Con.tButtonName[1] ="B";
            Transfer_Con.tButtonName[2] ="X";
            Transfer_Con.tButtonName[3] ="Y";
            Transfer_Con.tButtonName[4] ="UP";
            Transfer_Con.tButtonName[5] ="DOWN";
            Transfer_Con.tButtonName[6] ="LEFT";
            Transfer_Con.tButtonName[7] ="RIGHT";
            Transfer_Con.tButtonName[8] ="L1";
            Transfer_Con.tButtonName[9] ="R1";
            Transfer_Con.tButtonName[10] ="L2";
            Transfer_Con.tButtonName[11] ="R2";
            Transfer_Con.tButtonName[12] ="S2";
            Transfer_Con.tButtonName[13] ="S1";
            Transfer_Con.tButtonName[14] ="S4";
            Transfer_Con.tButtonName[15] ="S3";
            Transfer_Con.tButtonName[16] ="L-S";
            Transfer_Con.tButtonName[17] ="R-S";
            Transfer_Con.tButtonName[18] ="L-S";
            Transfer_Con.tButtonName[19] ="R-S";
        }
    }
    public int getDeviceMaxButton()
    {
        return  TI_BUTTON_MAX;
    }
    public int getDeviceLeftRockerIndex()
    {
        return  TI_LEFT_ROCKER;
    }
    public int getDeviceReportMaxWidth()
    {
        return  DEVICE_REPORT_MAX_WIDTH;
    }
    public int getDeviceReportMaxHeight()
    {
        return  DEVICE_REPORT_MAX_HEIGHT;
    }



    public String getStrFwVer() {
        return strFwVer;
    }

    public void setStrFwVer(String str) {
        strFwVer = str;
    }


    public String getStrBuildDate() {
        return strBuildDate;
    }

    public void setStrBuildDate(String str) {
        strBuildDate = str;
    }

    public int getGameListTotalNum() {
        return mGameListTotalNum;
    }

    public void setGameListTotalNum(int val) {
        mGameListTotalNum = val;
    }

    public int getGameListTotalNumKB() {
        return mGameListTotalNumKB;
    }

    public void setGameListTotalNumKB(int val) {
        mGameListTotalNumKB = val;
    }

    public int getGameGpTotalNum() {
        return mGameGpTotalNum;
    }

    public void setGameGpTotalNum(int val) {
        mGameGpTotalNum = val;
    }

    public int getGameGpTotalNumKB() {
        return mGameGpTotalNumKB;
    }

    public void setGameGpTotalNumKB(int val) {
        mGameGpTotalNumKB = val;
    }


    public int getGameDefExist() {
        return mGameDefExist;
    }
    public void setGameDefExist(int val) {
        mGameDefExist = val;
    }

    public int getGameDefExistKB() {
        return mGameDefExistKB;
    }
    public void setGameDefExistKB(int val) {
        mGameDefExistKB = val;
    }

    public int getUserGameNum() {
        return mUserGameNum;
    }
    public void setUserGameNum(int val) {
        mUserGameNum = val;
    }

    public int getUserGameNumKB() {
        return mUserGameNumKB;
    }
    public void setUserGameNumKB(int val) {
        mUserGameNumKB = val;
    }

    public String getUserGameName(int idx) {
        return (new String(cUserEditGame[idx]));
    }
    public void setUserGameName(int idx, String name) {
        cUserEditGame[idx] = name.toCharArray();
    }

    public String getPackageName(int index) {
        return packageName[index];
    }
    public void setPackageName(int index, String name) {
        packageName[index] = name;
    }

    public String getPackageNameKB(int index) {
        return packageNameKB[index];
    }
    public void setPackageNameKB(int index, String name) {
        packageNameKB[index] = name;
    }

    public String getUserGameNameKB(int idx) {
        return strUserGameNameKB[idx];
    }
    public void setUserGameNameKB(int idx, String name) {
        strUserGameNameKB[idx] = name;
    }


    public int getPt_Tbl_View_Size() {
        return mPt_Tbl_View_Size;
    }
    public void setPt_Tbl_View_Size(int val) {
        mPt_Tbl_View_Size = val;
    }

    public int getPt_Tbl_View_Alpha() {
        return mPt_Tbl_View_Alpha;
    }
    public void setPt_Tbl_View_Alpha(int val) {
        mPt_Tbl_View_Alpha = val;
    }

    public int getFloat_Icon_Size() {
        return mFloatLogoSize;
    }
    public void setFloat_Icon_Size(int val) {
        mFloatLogoSize = val;
    }

    public int getNormal_Icon_Size() {
        return mNormalIconSize;
    }
    public void setNormal_Icon_Size(int val) {
        mNormalIconSize = val;
    }

    public int getNormal_Icon_Alpha() {
        return mNormalIconAlpha;
    }
    public void setNormal_Icon_Alpha(int val) {
        mNormalIconAlpha = val;
    }

    public boolean getIsAutoSwitchToHDMI() {
        return bIsAutoSwitchToHDMI;
    }
    public void setIsAutoSwitchToHDMI(boolean val) {
        bIsAutoSwitchToHDMI = val;
    }

    public boolean getIsManualSetToHDMI() {
        return bIsManualSetToHDMI;
    }
    public void setIsManualSetToHDMI(boolean val) {
        bIsManualSetToHDMI = val;
    }

    public boolean getIsForceViewOut() {
        return bIsForceViewOut;
    }
    public void setIsForceViewOut(boolean val) {
        bIsForceViewOut = val;
    }

    public boolean getIsUseVirtualMouse() {return bIsVirtualMouse; }
    public void setIsUseVirtualMouse(boolean val) {
        bIsVirtualMouse = val;
    }
    public int getVirtualMouseInterval() {return mVirtualMouseInterval;};
    public void setVirtualMouseInterval(int val){mVirtualMouseInterval =val;}

    public int getManualHDMIWidth()
    {
        return mManualSetHDMIWidth;
    }
    public void setManualSetHDMIWidth(int width)
    {
        mManualSetHDMIWidth = width;
    }

    public int getManualHDMIHeight()
    {
        return mManualSetHDMIHeight;
    }
    public void setManualSetHDMIHeight(int height)
    {
        mManualSetHDMIHeight = height;
    }

    public int getRockerMoveRange_KB()
    {
        return mRockerMoveRange_KB;
    }
    public void setRockerMoveRange_KB(int val)
    {
        mRockerMoveRange_KB =val;
    }

    public int getSwitchToMouseKey_KB()
    {
        return mSwitchToMouseKey_kb;
    }

    public int switchMouseToKeyEvent(int group,int key) {
        int index = 0x29;
        if ((group & 0x7f) == 0) {
            switch (key & 0xff) {
                case 0x01:
                    index = 0x80;
                    break;
                case 0x02:
                    index = 0x81;
                    break;
                case 0x04:
                    index = 0x82;
                    break;
                case 0x08:
                    index = 0x83;
                    break;
                case 0x10:
                    index = 0x84;
                    break;
                case 0x20:
                    index = 0x85;
                    break;
                case 0x40:
                    index = 0x86;
                    break;
                case 0x80:
                    index = 0x87;
                    break;
            }
        } else if ((group & 0x7f) == 1) {
            index = key & 0xff;
        } else if ((group & 0x7f) == 2) {
            switch (key & 0xff) {
                case 0x01:
                    index = 0x8A;
                    break;
                case 0x02:
                    index = 0x8B;
                    break;
                case 0x04:
                    index = 0x8C;
                    break;
                case 0x08:
                    index = 0x8D;
                    break;
                case 0x10:
                    index = 0x8E;
                    break;
                case 0x20:
                    index = 0x8F;
                    break;
            }

        }
        return index;
    }

    public void setSwitchToMouseKey_KB(int val)
    {
        mSwitchToMouseKey_kb =val;
    }




    public boolean getIsAudioOut() {return bIsAudioOut;}
    public void setIsAudioOut(boolean val)
    {
        bIsAudioOut =val;
    }

    void setUsbEpIn(UsbEndpoint epVal) {
        epIn = epVal;
    }

    void setDeviceConnection(UsbDeviceConnection epVal) {
        DeviceConnection = epVal;
    }

    void setUsbEpOut(UsbEndpoint epVal) {
        epOut = epVal;
    }

    void setbIsUsbConnect(boolean val) {
        bIsUsbConnect = val;
    }

    boolean getbIsUsbConnect() {
        return bIsUsbConnect;
    }

    void setbIsTransferMode(boolean val) {
        bIsTransferMode = val;
    }

    boolean getbIsTransferMode() {
        return bIsTransferMode;
    }

    void setIsPortraitGame(boolean val) {
        bIsPortraitGame = val;
    }

    boolean getIsPortraitGame() {
        return bIsPortraitGame;
    }

    // Is Notch
    void setIsNotchFlag(boolean val) {
        bIsNotchFlag = val;
    }

    boolean getIsNotchFlag() {
        return bIsNotchFlag;
    }

    public boolean hasNotchScreen(Activity activity) {
        if ((isAndroidPNotch(activity) != null) || hasNotchAtHuawei(activity) || (getXiaomiInt("ro.miui.notch", activity) == 1)
                || hasNotchAtOppo(activity) || hasNotchAtVivo(activity)) {
            bIsNotchFlag = true;
            return true;
        }
        return false;
    }

    // Android P
    public static DisplayCutout isAndroidPNotch(Activity activity) {
        View decorView = activity.getWindow().getDecorView();
        if ((decorView != null) && (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)) {
            WindowInsets windowInsets = decorView.getRootWindowInsets();
            if (windowInsets != null)
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    return windowInsets.getDisplayCutout();
                }
        }
        return null;
    }

    // xiaomi
    public static boolean isXiaomi() {
        return "Xiaomi".equals(Build.MANUFACTURER);
    }

    public static int getXiaomiInt(String key, Activity activity) {
        int result = 0;
        if (isXiaomi()) {
            try {
                ClassLoader classLoader = activity.getClassLoader();
                @SuppressWarnings("rawtypes")
                Class SystemProperties = classLoader.loadClass("android.os.SystemProperties");
                @SuppressWarnings("rawtypes")
                Class[] paramTypes = new Class[2];
                paramTypes[0] = String.class;
                paramTypes[1] = int.class;
                Method getInt = SystemProperties.getMethod("getInt", paramTypes);
                Object[] params = new Object[2];
                params[0] = new String(key);
                params[1] = new Integer(0);
                result = (Integer) getInt.invoke(SystemProperties, params);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    // Huawei
    public static boolean hasNotchAtHuawei(Context context) {
        boolean ret = false;
        try {
            ClassLoader classLoader = context.getClassLoader();
            Class hwNotchSizeUtil = classLoader.loadClass("com.huawei.android.util.HwNotchSizeUtil");
            Method get = hwNotchSizeUtil.getMethod("hasNotchInScreen");
            ret = (boolean) get.invoke(hwNotchSizeUtil);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return ret;
    }

    // Vivo
    public static final int VIVO_NOTCH = 0x00000020;
    public static final int VIVO_FILLET = 0x00000008;

    public static boolean hasNotchAtVivo(Context context) {
        boolean ret = false;
        try {
            ClassLoader classLoader = context.getClassLoader();
            Class FtFeature = classLoader.loadClass("android.util.FtFeature");
            Method get = FtFeature.getMethod("isFeatureSupport", int.class);
            ret = (boolean) get.invoke(FtFeature, VIVO_NOTCH);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return ret;
    }

    // Oppo
    public static boolean hasNotchAtOppo(Context context) {
        return context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
    }

    // User Guide
    void setIsEnterAdvGuide(boolean val) {
        bIsEnterAdvGuide = val;
    }

    boolean getIsEnterAdvGuide() {
        return bIsEnterAdvGuide;
    }

    void setIsEnterGameGuide(boolean val) {
        bIsEnterGameGuide = val;
    }

    boolean getIsEnterGameGuide() {
        return bIsEnterGameGuide;
    }

    void setIsEnterGameNeedGuide(boolean val) {
        bIsEnterGameNeedGuide = val;
    }

    boolean getIsEnterGameNeedGuide() {
        return bIsEnterGameNeedGuide;
    }

    void setIsAdvSetNeedGuide(boolean val) {
        bIsAdvSetNeedGuide = val;
    }

    boolean getIsAdvSetNeedGuide() {
        return bIsAdvSetNeedGuide;
    }


    // Transfer

    boolean assignEndpoint() {
        if (myInterface.getEndpoint(1) != null) {
            epOut = myInterface.getEndpoint(1);
        } else {
            bIsUsbConnect = false;
            return false;
        }
        if (myInterface.getEndpoint(0) != null) {
            epIn = myInterface.getEndpoint(0);
        } else {
            bIsUsbConnect = false;
            return false;
        }
        bIsUsbConnect = true;
        Log.d(TAG, "assignEndpoint");
        return true;
    }

    boolean openDevice() {
        if (myInterface != null) {
            UsbDeviceConnection conn = null;
            if (myUsbManager.hasPermission(myUsbDevice)) {
                conn = myUsbManager.openDevice(myUsbDevice);
            } else {
                if(getLastConnectDeviceIndex()!=Transfer_Con.DEVICE_MP1_INDEX)
                    Toast.makeText(this, "No Permission", Toast.LENGTH_SHORT).show();
            }
            if (conn == null) {
                return false;
            }

            if (conn.claimInterface(myInterface, true)) {
                DeviceConnection = conn; // 到此你的android设备已经连上HID设备
                return true;
            } else {
                conn.close();
            }
        }
        return false;
    }

    boolean findInterface() {
        if (myUsbDevice != null) {
            for (int i = 0; i < myUsbDevice.getInterfaceCount(); i++) {
                UsbInterface intf = myUsbDevice.getInterface(i);
                if (intf.getInterfaceClass() == INTERFACE_CLASS
                        && intf.getInterfaceSubclass() == INTERFACE_SUBCLASS
                        && intf.getInterfaceProtocol() == INTERFACE_PROTOCOL) {
                    myInterface = intf;
                    return true;
                }
            }
        }
        return false;
    }

    boolean isUSBDeviceListEmpty()
    {
        if (myUsbManager == null)
            return false;
        HashMap<String, UsbDevice> deviceList = myUsbManager.getDeviceList();
        return deviceList.isEmpty();
    }

    int enumDevice() {
        if (myUsbManager == null)
            return -1;
        Log.d(TAG, "enumDevice " + myUsbManager.toString());
        HashMap<String, UsbDevice> deviceList = myUsbManager.getDeviceList();
        if (!deviceList.isEmpty()) {
            StringBuffer sb = new StringBuffer();
            for (UsbDevice device : deviceList.values()) {
                sb.append(device.toString());
                sb.append("\n");
                // 枚举到设备
                Log.d(TAG,String.format("vid=%x,Pid=%x",device.getVendorId(),device.getProductId()));
                if (device.getVendorId() == Transfer_Con.VendorID) {

                    if (device.getProductId() == ISPCmd_Con.ProductID) {
                        bIsTransferMode = false;
                        //Toast.makeText(this,"ISP Connect",Toast.LENGTH_SHORT).show();
                        Toast.makeText(this, getResources().getString(R.string.string_pop_update_mode), Toast.LENGTH_SHORT).show();
                        myUsbDevice = device;
                        return 1;
                    } else if ((device.getProductId() == Transfer_Con.ProductID)||(device.getProductId() == Transfer_Con.ProductID_HN2)||(device.getProductId() == Transfer_Con.ProductID_MP1)||(device.getProductId() == Transfer_Con.ProductID_BX1)||(device.getProductId() == Transfer_Con.ProductID_BX2)||(device.getProductId() == Transfer_Con.ProductID_U41)||(device.getProductId() == Transfer_Con.ProductID_MR1)) {
                        bIsTransferMode = true;
                        //Toast.makeText(this,"正常模式",Toast.LENGTH_SHORT).show();
                        myUsbDevice = device;
                        return 1;
                    }
                }
                else if (device.getVendorId() == Transfer_Con.VendorID_360) {
                    if (device.getProductId() == Transfer_Con.ProductID_360) {
                        bIsTransferMode = true;
                        //Toast.makeText(this,"正常模式",Toast.LENGTH_SHORT).show();
                        myUsbDevice = device;
                        return 1;
                    }
                }
                else
                {
                    for(int a=0;a<10;a++)
                    {
                        if (device.getVendorId() == Transfer_Con.u16VIDTable[a])
                        {
                            for(int k=0;k<30;k++)
                            {
                                if (device.getProductId() == Transfer_Con.u16PIDTable[k])
                                {
                                    bIsTransferMode = true;
                                    myUsbDevice = device;
                                    return 1;
                                }
                            }
                        }
                    }
                }
            }
        }
        return 0;
    }
    boolean connectDevice() {
        if (bIsUsbConnect)
            return true;
        if (enumDevice() > 0) {
            if (findInterface()) {
                if (openDevice()) {
                    return assignEndpoint();
                }
            }
        }
        return false;
    }
    int Checksum(byte[] buf, int len) {
        int i;
        int c;
        for (c = 0, i = 0; i < len; i++) {
            c += (int) (buf[i] & 0xff);
        }
        return (c);
    }

    void IntToByte(int src, byte[] tar, int start) {
        tar[start] = (byte) (src & 0xFF);
        tar[start + 1] = (byte) ((src & 0xFF00) >> 8);
        tar[start + 2] = (byte) ((src & 0xFF0000) >> 16);
        tar[start + 3] = (byte) ((src & 0xFF000000) >> 24);
    }

    void getVerData(byte[] pReadBuf, byte len) {
        byte temp[] = new byte[len - 2];
        System.arraycopy(pReadBuf, 1, temp, 0, len - 2);
        String str = new String(temp);
        Log.d(TAG, str);
        String[] str1 = str.split("\\.");
        m_u32CodeVer = Integer.parseInt(str1[0]);
        m_u32CodeVer = (m_u32CodeVer << 16) + Integer.parseInt(str1[1]);
        m_u32CodeVer = (m_u32CodeVer << 8) + Integer.parseInt(str1[2]);
    }

    int get_Version(byte[] pReadBuf) {
        int mRet;
        if (!bIsTransferMode)
            return 0;
        if (!bIsUsbConnect) {
            Log.d(TAG, "Can't Open HID Device");
            return 0;
        } else {
            if(Transfer_Con.ENABLE_SUPPORT_FULL_CMD)
                DataBuf[0] = (byte)(Transfer_Con.HID_CMD_GET_VERSION);
            else
                DataBuf[0] = (byte) (0x80 | Transfer_Con.HID_CMD_GET_VERSION);
            DataBuf[1] = 14;//
            IntToByte(0, DataBuf, 2);
            IntToByte(0, DataBuf, 6);
            IntToByte(Transfer_Con.HID_CMD_SIGNATURE, DataBuf, 10);
            int cks = Checksum(DataBuf, 14);
            IntToByte(cks, DataBuf, 14);
            int ret = DeviceConnection.bulkTransfer(epOut, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);
            if (ret != Transfer_Con.HID_PACKET_SIZE) {
                Log.d(TAG, "Send command Fail!");
                return 0;
            }

            //while (true)
            {
                int re = DeviceConnection.bulkTransfer(epIn, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);
                if (re < 18) {
                    Log.d(TAG, "Get Data Fail! er=" + String.valueOf(re));
                    return 0;
                } else {
                    cks = Checksum(DataBuf, 14);
                    int getcks = (DataBuf[14]&0xff)+((DataBuf[15]&0xff)<<8)+((DataBuf[16]&0xff)<<16)+((DataBuf[17]&0xff)<<24);

                    if ((DataBuf[0] == Transfer_Con.HID_CMD_GET_VERSION) && (DataBuf[2] != 0x00)&&(cks==getcks)) {
                        mRet = DataBuf[2];
                        System.arraycopy(DataBuf, 18, pReadBuf, 0, DataBuf[2]);
                        pReadBuf[DataBuf[2]] = 0;
                        getVerData(pReadBuf, DataBuf[2]);
                    } else {
                        mRet = 0;
                        Log.d(TAG, "Get Version fail!");
                    }
                   // break;
                }
            }
        }
        return mRet;
    }

    public void clear_IntVersion()
    {
        m_u32CodeVer = 1;
    }

    int get_IntVersion() {
        return m_u32CodeVer;
    }

    int get_BuildDate(byte[] pReadBuf) {
        int mRet;
        if (!bIsTransferMode)
            return 0;
        if (!bIsUsbConnect) {
            Log.d(TAG, "Can't Open HID Device");
            return 0;
        } else {
            if(Transfer_Con.ENABLE_SUPPORT_FULL_CMD)
                DataBuf[0] = (byte)(Transfer_Con.HID_CMD_GET_DATE);
            else
                DataBuf[0] = (byte) (0x80 | Transfer_Con.HID_CMD_GET_DATE);
            DataBuf[1] = 14;//
            IntToByte(0, DataBuf, 2);
            IntToByte(0, DataBuf, 6);
            IntToByte(Transfer_Con.HID_CMD_SIGNATURE, DataBuf, 10);
            int cks = Checksum(DataBuf, 14);
            IntToByte(cks, DataBuf, 14);
            int ret = DeviceConnection.bulkTransfer(epOut, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);

            if (ret != Transfer_Con.HID_PACKET_SIZE) {
                Log.d(TAG, "Send command Fail!");
                return 0;
            }

            //while(true)
            {
                int re = DeviceConnection.bulkTransfer(epIn, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);
                if (re < 18) {
                    Log.d(TAG, "Get Data Fail! er=" + String.valueOf(re));
                    return 0;
                } else {
                    cks = Checksum(DataBuf, 14);
                    int getcks = (DataBuf[14]&0xff)+((DataBuf[15]&0xff)<<8)+((DataBuf[16]&0xff)<<16)+((DataBuf[17]&0xff)<<24);
                    if ((DataBuf[0] == Transfer_Con.HID_CMD_GET_DATE) && (DataBuf[2] != 0x00)&&(cks==getcks)) {
                        mRet = DataBuf[2];
                        System.arraycopy(DataBuf, 18, pReadBuf, 0, DataBuf[2]);
                        pReadBuf[DataBuf[2]] = 0;
                        //for(int a=0;a<)
                        Log.d(TAG, "Get Build date Success!");
                    } else {
                        mRet = 0;
                        Log.d(TAG, "Get Build date  fail!");
                    }
                    //break;
                }
            }
        }
        return mRet;
    }

    int get_DeviceName(byte[] pReadBuf) {
        int mRet;
        if (!bIsTransferMode)
            return 0;
        if (!bIsUsbConnect) {
            Log.d(TAG, "Can't Open HID Device");
            return 0;
        } else {
            if(Transfer_Con.ENABLE_SUPPORT_FULL_CMD)
                DataBuf[0] = (byte)(Transfer_Con.HID_CMD_GET_NAME);
            else
                DataBuf[0] = (byte) (0x80 | Transfer_Con.HID_CMD_GET_NAME);
            DataBuf[1] = 14;//
            IntToByte(0, DataBuf, 2);
            IntToByte(0, DataBuf, 6);
            IntToByte(Transfer_Con.HID_CMD_SIGNATURE, DataBuf, 10);
            int cks = Checksum(DataBuf, 14);
            IntToByte(cks, DataBuf, 14);
            int ret = DeviceConnection.bulkTransfer(epOut, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);

            if (ret != Transfer_Con.HID_PACKET_SIZE) {
                Log.d(TAG, "Send command Fail!");
                return 0;
            }

            //while(true)
            {
                int re = DeviceConnection.bulkTransfer(epIn, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);
                if (re < 18) {
                    Log.d(TAG, "Get Data Fail! er=" + String.valueOf(re));
                    return 0;
                } else {
                    cks = Checksum(DataBuf, 14);
                    int getcks = (DataBuf[14]&0xff)+((DataBuf[15]&0xff)<<8)+((DataBuf[16]&0xff)<<16)+((DataBuf[17]&0xff)<<24);
                    if ((DataBuf[0] == Transfer_Con.HID_CMD_GET_NAME) && (DataBuf[2] != 0x00)&&(cks==getcks)) {
                        mRet = DataBuf[2];
                        System.arraycopy(DataBuf, 18, pReadBuf, 0, DataBuf[2]);
                        pReadBuf[DataBuf[2]] = 0;
                        //for(int a=0;a<)
                        Log.d(TAG, "Get Device Name Success!");
                    } else {
                        mRet = 0;
                        Log.d(TAG, "Get Device Name fail!");
                    }
                    //break;
                }
            }
        }
        return mRet;
    }


    boolean get_CurPhone(byte[] pReadBuf) {
        boolean bRet;
        if (!bIsTransferMode)
            return false;
        if (!bIsUsbConnect) {
            Log.d(TAG, "Can't Open HID Device");
            return false;
        } else {
            if(Transfer_Con.ENABLE_SUPPORT_FULL_CMD)
                DataBuf[0] = (byte)(Transfer_Con.HID_CMD_GET_PHONE_CUR);
            else
                DataBuf[0] = (byte) (0x80 | Transfer_Con.HID_CMD_GET_PHONE_CUR);
            DataBuf[1] = 14;//
            IntToByte(0, DataBuf, 2);
            IntToByte(0, DataBuf, 6);
            IntToByte(Transfer_Con.HID_CMD_SIGNATURE, DataBuf, 10);
            int cks = Checksum(DataBuf, 14);
            IntToByte(cks, DataBuf, 14);
            int ret = DeviceConnection.bulkTransfer(epOut, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);

            if (ret != Transfer_Con.HID_PACKET_SIZE) {
                Log.d(TAG, "Send command Fail!");
                return false;
            }
            //while(true)
            {
                int re = DeviceConnection.bulkTransfer(epIn, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);
                if (re < 18) {
                    Log.d(TAG, "Get Data Fail! er=" + String.valueOf(re));
                    return false;
                } else {
                    if ((DataBuf[0] == (byte) Transfer_Con.HID_CMD_GET_PHONE_CUR)) {
                        bRet = true;
                        pReadBuf[0] = DataBuf[2];
                        //for(int a=0;a<)
                        Log.d(TAG, "Get Current Phone Success!");
                    } else {
                        bRet = false;
                        Log.d(TAG, "Get Current Phone  fail!");
                    }
                    // break;
                }
            }
        }
        return bRet;
    }

    boolean get_CurGame(byte[] pReadBuf) {
        boolean bRet;
        if (!bIsTransferMode)
            return false;
        if (!bIsUsbConnect) {
            Log.d(TAG, "Can't Open HID Device");
            return false;
        } else {
            if(Transfer_Con.ENABLE_SUPPORT_FULL_CMD)
                DataBuf[0] = (byte)(Transfer_Con.HID_CMD_GET_GAME_CUR);
            else
                DataBuf[0] = (byte) (0x80 | Transfer_Con.HID_CMD_GET_GAME_CUR);
            DataBuf[1] = 14;//
            IntToByte(0, DataBuf, 2);
            IntToByte(0, DataBuf, 6);
            IntToByte(Transfer_Con.HID_CMD_SIGNATURE, DataBuf, 10);
            int cks = Checksum(DataBuf, 14);
            IntToByte(cks, DataBuf, 14);
            int ret = DeviceConnection.bulkTransfer(epOut, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);

            if (ret != Transfer_Con.HID_PACKET_SIZE) {
                Log.d(TAG, "Send command Fail!");
                return false;
            }
            //while(true)
            {
                int re = DeviceConnection.bulkTransfer(epIn, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);
                if (re < 18) {
                    Log.d(TAG, "Get Data Fail! er=" + String.valueOf(re));
                    return false;
                } else {
                    if ((DataBuf[0] == (byte) Transfer_Con.HID_CMD_GET_GAME_CUR)) {
                        pReadBuf[0] = DataBuf[2];
                        bRet = true;
                        //for(int a=0;a<)
                        Log.d(TAG, "Get Current Game Success!");
                    } else {
                        bRet = false;
                        Log.d(TAG, "Get Current Game  fail!");
                    }
                    //break;
                }
            }
        }
        return bRet;
    }

    boolean get_CurPtTbl(byte size, byte offset, byte[] pReadBuf) {
        boolean bRet;
        if (!bIsTransferMode)
            return false;
        if (!bIsUsbConnect) {
            Log.d(TAG, "Can't Open HID Device");
            return false;
        } else {
            if(Transfer_Con.ENABLE_SUPPORT_FULL_CMD)
                DataBuf[0] = (byte)(Transfer_Con.HID_CMD_GET_POINT_TBL);
            else
                DataBuf[0] = (byte) (0x80 | Transfer_Con.HID_CMD_GET_POINT_TBL);
            DataBuf[1] = 14;//
            IntToByte(size, DataBuf, 2);
            IntToByte(offset, DataBuf, 6);
            IntToByte(Transfer_Con.HID_CMD_SIGNATURE, DataBuf, 10);
            int cks = Checksum(DataBuf, 14);
            IntToByte(cks, DataBuf, 14);
            int ret = DeviceConnection.bulkTransfer(epOut, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);

            if (ret != Transfer_Con.HID_PACKET_SIZE) {
                Log.d(TAG, "Send command Fail!");
                return false;
            }
            //while(true)
            {
                int re = DeviceConnection.bulkTransfer(epIn, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);
                if (re < 18) {
                    Log.d(TAG, "Get Data Fail! er=" + String.valueOf(re));
                    return false;
                } else {
                    if ((DataBuf[0] == (byte) (Transfer_Con.HID_CMD_GET_POINT_TBL))) {
                        System.arraycopy(DataBuf, 18, pReadBuf, 0, size);
                        bRet = true;
                    } else {
                        bRet = false;
                        Log.d(TAG, "Get position table  fail!");
                    }
                    //break;
                }
            }
        }
        return bRet;
    }

    void getCurCorTbl() {
        byte []u8Temp = new byte[Transfer_Con.HID_PACKET_SIZE];
     if(get_CurPtTbl((byte)(TI_BUTTON_MAX*2),(byte)0,u8Temp))
    {
        short a;
        for (a = 0; a < TI_BUTTON_MAX; a++) {
            ptTbl[a] = (int) (u8Temp[2 * a + 1] & 0xff) << 8;
            ptTbl[a] += (int) (u8Temp[2 * a] & 0xff);
        }
        if (get_CurPtTbl((byte) (TI_BUTTON_MAX * 2), (byte) 1, u8Temp)) {
            for (a = 0; a < TI_BUTTON_MAX; a++) {
                ptTbl[TI_BUTTON_MAX + a] = (int) (u8Temp[2 * a + 1] & 0xff) << 8;
                ptTbl[TI_BUTTON_MAX + a] += (int) (u8Temp[2 * a] & 0xff);
                //str+=String.valueOf(ptTbl[mGlobalVar.getDeviceMaxButton()+a])+", ";
            }
        }
    }
}

    boolean set_CurPtTbl(byte size,byte offset, byte[] pReadBuf)
    {
        boolean bRet;
        if(!bIsTransferMode)
            return false;
        if(!bIsUsbConnect)
        {
            Log.d(TAG, "Can't Open HID Device");
            return false;
        }
        else
        {
            if(Transfer_Con.ENABLE_SUPPORT_FULL_CMD)
                DataBuf[0] = (byte)(Transfer_Con.HID_CMD_SET_POINT_TBL);
            else
                DataBuf[0] = (byte)(0x80|Transfer_Con.HID_CMD_SET_POINT_TBL);
            DataBuf[1] = 14;//
            IntToByte(size,DataBuf,2 );
            IntToByte(offset,DataBuf,6 );
            IntToByte(Transfer_Con.HID_CMD_SIGNATURE,DataBuf,10 );
            int cks = Checksum(DataBuf, 14);
            IntToByte(cks,DataBuf,14 );
            System.arraycopy(pReadBuf,0,DataBuf,18,size);
            int	 ret = DeviceConnection.bulkTransfer(epOut, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);

            if(ret!=Transfer_Con.HID_PACKET_SIZE)
            {
                Log.d(TAG, "Send command Fail!");
                return  false;
            }
            //while(true)
            {
                int re = DeviceConnection.bulkTransfer(epIn, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);
                if(re<18)
                {
                    Log.d(TAG, "Get Data Fail! er="+String.valueOf(re));
                    return  false;
                }
                else
                {
                    bRet =  true;
                    //break;
                }
            }
        }
        return bRet;
    }

    boolean tranGetComboTbl(byte table, byte size,byte offset, byte[] pReadBuf)
    {
        boolean bRet;
        if (!bIsTransferMode)
            return false;
        if (!bIsUsbConnect) {
            Log.d(TAG, "Can't Open HID Device");
            return false;
        } else {
            if(Transfer_Con.ENABLE_SUPPORT_FULL_CMD)
                DataBuf[0] = (byte)(Transfer_Con.HID_CMD_GET_COMBO_PTTBL);
            else
                DataBuf[0] = (byte) (0x80 | Transfer_Con.HID_CMD_GET_COMBO_PTTBL);
            DataBuf[1] = 14;//
            IntToByte(table, DataBuf, 2);
            int temp= (size<<16)+offset;
            IntToByte(temp, DataBuf, 6);
            IntToByte(Transfer_Con.HID_CMD_SIGNATURE, DataBuf, 10);
            int cks = Checksum(DataBuf, 14);
            IntToByte(cks, DataBuf, 14);
            int ret = DeviceConnection.bulkTransfer(epOut, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);

            if (ret != Transfer_Con.HID_PACKET_SIZE) {
                Log.d(TAG, "Send command Fail!");
                return false;
            }
            //while(true)
            {
                int re = DeviceConnection.bulkTransfer(epIn, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);
                if (re < 18) {
                    Log.d(TAG, "Get Data Fail! er=" + String.valueOf(re));
                    return false;
                } else {
                    if ((DataBuf[0] == (byte) (Transfer_Con.HID_CMD_GET_COMBO_PTTBL))) {
                        System.arraycopy(DataBuf, 18, pReadBuf, 0, size);
                        bRet = true;
                    } else {
                        bRet = false;
                        Log.d(TAG, "Get position table  fail!");
                    }
                    //break;
                }
            }
        }
        return bRet;
    }

    boolean tranSetComboTbl(byte table, byte size,byte offset, byte[] pReadBuf)
    {
        boolean bRet;
        if (!bIsTransferMode)
            return false;
        if (!bIsUsbConnect) {
            Log.d(TAG, "Can't Open HID Device");
            return false;
        } else {
            if(Transfer_Con.ENABLE_SUPPORT_FULL_CMD)
                DataBuf[0] = (byte)(Transfer_Con.HID_CMD_SET_COMBO_PTTBL);
            else
                DataBuf[0] = (byte)(0x80|Transfer_Con.HID_CMD_SET_COMBO_PTTBL);
            DataBuf[1] = 14;//
            IntToByte(table,DataBuf,2 );
            int temp= (size<<16)+offset;
            IntToByte(temp,DataBuf,6 );
            IntToByte(Transfer_Con.HID_CMD_SIGNATURE,DataBuf,10 );
            int cks = Checksum(DataBuf, 14);
            IntToByte(cks,DataBuf,14 );
            System.arraycopy(pReadBuf,0,DataBuf,18,size);
            int	 ret = DeviceConnection.bulkTransfer(epOut, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);

            if(ret!=Transfer_Con.HID_PACKET_SIZE)
            {
                Log.d(TAG, "Send command Fail!");
                return  false;
            }
            //while(true)
            {
                int re = DeviceConnection.bulkTransfer(epIn, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);
                if(re<18)
                {
                    Log.d(TAG, "Get Data Fail! er="+String.valueOf(re));
                    return  false;
                }
                else
                {
                    bRet =  true;
                    //break;
                }
            }
        }
        return bRet;
    }


    boolean tranSet_Value(int cmd,int data1,int data2)
    {
        boolean bRet;
        if(!bIsTransferMode)
            return false;
        if(!bIsUsbConnect)
        {
            Log.d(TAG, "Can't Open HID Device");
            return false;
        }
        else
        {
            if(Transfer_Con.ENABLE_SUPPORT_FULL_CMD)
                DataBuf[0] = (byte)(cmd);
            else
                DataBuf[0] = (byte)(0x80|cmd);
            DataBuf[1] = 14;//
            IntToByte(data1,DataBuf,2 );
            IntToByte(data2,DataBuf,6 );
            IntToByte(Transfer_Con.HID_CMD_SIGNATURE,DataBuf,10 );
            int cks = Checksum(DataBuf, 14);
            IntToByte(cks,DataBuf,14 );
            int	 ret = DeviceConnection.bulkTransfer(epOut, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);

            if(ret!=Transfer_Con.HID_PACKET_SIZE)
            {
                Log.d(TAG, "tranSet_Value Fail! cmd="+String.format("0x%x.",cmd));
                return  false;
            }
            if(cmd==Transfer_Con.HID_CMD_FW_RESET)
            {
                return true;
            }
            //while(true)
            {
                int re = DeviceConnection.bulkTransfer(epIn, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);
                if(re<18)
                {
                    Log.d(TAG, "tran Set Vaule read back fail! er="+String.valueOf(re));
                    return  false;
                }
                else
                {
                    cks = Checksum(DataBuf, 14);
                    int getcks = (DataBuf[14]&0xff)+((DataBuf[15]&0xff)<<8)+((DataBuf[16]&0xff)<<16)+((DataBuf[17]&0xff)<<24);
                    //Log.d(TAG, "cmd="+String.format("0x%x,cks=%x,%x",cmd,cks,getcks));
                    if((DataBuf[0]==(byte)cmd)&&(cks==getcks)) {
                        bRet = true;
                    }
                    else {
                        bRet = false;
                        Log.d(TAG, "tran Set Vaule read  error! cmd="+String.format("0x%x.",cmd));
                    }
                    //break;
                }
            }
        }
        return bRet;
    }

    boolean tranSet_Value_retry(int cmd,int data1,int data2,int retry)
    {
        for(int a=0;a<(retry+1);a++)
        {
           if(tranSet_Value(cmd,data1,data2))
               return true;
        }
        return false;
    }


    boolean tranGet_Value_status(int cmd,int data1,int data2,byte[] data)
    {
        boolean bRet;
        if(!bIsTransferMode)
            return false;
        if(!bIsUsbConnect)
        {
            Log.d(TAG, "Can't Open HID Device");
            return false;
        }
        else
        {
            int cks;
            //while(true)
            {
                int re = DeviceConnection.bulkTransfer(epIn, DataBuf, Transfer_Con.HID_PACKET_SIZE, 5);
                if(re<18)
                {
                    //Log.d(TAG, "1111111111 tran Get Vaule read back fail! er="+String.valueOf(re));
                    return  false;
                }
                else
                {
                    String str="--> ";
                    for(int a=0;a<18;a++)
                        str+=String.valueOf(DataBuf[a])+", ";

                    cks = Checksum(DataBuf, 14);
                    int getcks = (DataBuf[14]&0xff)+((DataBuf[15]&0xff)<<8)+((DataBuf[16]&0xff)<<16)+((DataBuf[17]&0xff)<<24);
                    if((DataBuf[0]==(byte)cmd)&&(cks==getcks)) {
                        System.arraycopy(DataBuf,2,data,0,re-2);
                        bRet = true;
                    }
                    else {
                        bRet = false;
                        Log.d(TAG, "tranGet_Value_status read back error! cmd="+String.format("0x%x",cmd)+str);
                    }
                    //break;
                }
            }
        }
        return bRet;
    }



    boolean tranGet_Value(int cmd,int data1,int data2,byte[] data)
    {
        boolean bRet;
        if(!bIsTransferMode)
            return false;
        if(!bIsUsbConnect)
        {
            Log.d(TAG, "Can't Open HID Device");
            return false;
        }
        else
        {
            if(Transfer_Con.ENABLE_SUPPORT_FULL_CMD)
                DataBuf[0] = (byte)(cmd);
            else
                DataBuf[0] = (byte)(0x80|cmd);
            DataBuf[1] = 14;//
            IntToByte(data1,DataBuf,2 );
            IntToByte(data2,DataBuf,6 );
            IntToByte(Transfer_Con.HID_CMD_SIGNATURE,DataBuf,10 );
            int cks = Checksum(DataBuf, 14);
            IntToByte(cks,DataBuf,14 );
            int	 ret = DeviceConnection.bulkTransfer(epOut, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);

            if(ret!=Transfer_Con.HID_PACKET_SIZE)
            {
                Log.d(TAG, "tranGet_Value! cmd="+String.format("0x%x",cmd));
                return  false;
            }
            //while(true)
            {
                int re = DeviceConnection.bulkTransfer(epIn, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);
                if(re<18)
                {
                    Log.d(TAG, "tran Get Vaule read back fail! er="+String.valueOf(re));
                    return  false;
                }
                else
                {
                    cks = Checksum(DataBuf, 14);
                    int getcks = (DataBuf[14]&0xff)+((DataBuf[15]&0xff)<<8)+((DataBuf[16]&0xff)<<16)+((DataBuf[17]&0xff)<<24);
                    if((DataBuf[0]==(byte)cmd)&&(cks==getcks)) {
                        System.arraycopy(DataBuf,2,data,0,re-2);
                        bRet = true;
                    }
                    else {
                        bRet = false;
                        Log.d(TAG, "tran Get Vaule read back error! cmd="+String.format("0x%x",cmd));
                    }
                    //break;
                }
            }
        }
        return bRet;
    }

    boolean tranGet_Value_retry(int cmd,int data1,int data2,byte[] data,int retry)
    {
        for(int a=0;a<(retry+1);a++)
        {
            if(tranGet_Value(cmd,data1,data2,data))
                return true;
        }
        return false;
    }


    boolean tranSet_MultiValue(int cmd,int data1,int data2,byte[]data)
    {
        boolean bRet;
        if(!bIsTransferMode)
            return false;
        if(!bIsUsbConnect)
        {
            Log.d(TAG, "Can't Open HID Device");
            return false;
        }
        else
        {
            if(Transfer_Con.ENABLE_SUPPORT_FULL_CMD)
                DataBuf[0] = (byte)(cmd);
            else
                DataBuf[0] = (byte)(0x80|cmd);
            DataBuf[1] = 14;//
            IntToByte(data1,DataBuf,2 );
            IntToByte(data2,DataBuf,6 );
            IntToByte(Transfer_Con.HID_CMD_SIGNATURE,DataBuf,10 );
            int cks = Checksum(DataBuf, 14);
            IntToByte(cks,DataBuf,14 );
            System.arraycopy(data,0,DataBuf,18,data2);
            int	 ret = DeviceConnection.bulkTransfer(epOut, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);

            if(ret!=Transfer_Con.HID_PACKET_SIZE)
            {
                Log.d(TAG, "tranSet_MultiValue Fail! cmd="+String.format("0x%x...ret=%x",cmd,ret));
                return  false;
            }
            if(cmd==Transfer_Con.HID_CMD_FW_RESET)
            {
                return true;
            }
            //while(true)
            {
                int re = DeviceConnection.bulkTransfer(epIn, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);
                if(re<18)
                {
                    Log.d(TAG, "tran Set Vaule read back fail! er="+String.valueOf(re));
                    return  false;
                }
                else
                {
                    cks = Checksum(DataBuf, 14);
                    int getcks = (DataBuf[14]&0xff)+((DataBuf[15]&0xff)<<8)+((DataBuf[16]&0xff)<<16)+((DataBuf[17]&0xff)<<24);
                    //Log.d(TAG, "cmd="+String.format("0x%x,cks=%x,%x",cmd,cks,getcks));
                    if((DataBuf[0]==(byte)cmd)&&(cks==getcks))  {
                        bRet = true;
                    }
                    else {
                        bRet = false;
                        Log.d(TAG, "tran Set Vaule read back error! cmd="+String.format("0x%x.",cmd));
                    }
                    //break;
                }
            }
        }
        return bRet;
    }

    boolean tranSet_MultiValue_retry(int cmd,int data1,int data2,byte[]data,int retry)
    {
        for(int a=0;a<(retry+1);a++)
        {
            if(tranSet_MultiValue(cmd,data1,data2,data))
                return true;
        }
        return false;
    }

    boolean Check_Connect(byte[] pReadBuf)
    {
        boolean bRet;
        if(!bIsTransferMode)
            return false;
        if(!bIsUsbConnect)
        {
            Log.d(TAG, "Can't Open HID Device");
            return false;
        }
        else
        {
            if(Transfer_Con.ENABLE_SUPPORT_FULL_CMD)
                DataBuf[0] = (byte)(Transfer_Con.HID_CMD_CHECK_CONNECT);
            else
                DataBuf[0] = (byte)(Transfer_Con.HID_CMD_CHECK_CONNECT|0x80);
            DataBuf[1] = 14;//
            IntToByte(0,DataBuf,2 );
            IntToByte(0,DataBuf,6 );
            IntToByte(Transfer_Con.HID_CMD_SIGNATURE,DataBuf,10 );
            int cks = Checksum(DataBuf, 14);
            IntToByte(cks,DataBuf,14 );
            int	 ret = DeviceConnection.bulkTransfer(epOut, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);
            if(ret!=Transfer_Con.HID_PACKET_SIZE)
            {
                Log.d(TAG, "Send command Fail!");
                return  false;
            }

            //while(true)
            {
                int re = DeviceConnection.bulkTransfer(epIn, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);
                if(re<18)
                {
                    Log.d(TAG, "Get Data Fail! er="+String.valueOf(re));
                    return  false;
                }
                else
                {
                    if(DataBuf[2]==0x01) {
                        bRet = true;
                        Log.d(TAG, "Connect Success!");
                    }
                    else {
                        bRet = false;
                        Log.d(TAG, "Connect fail!");
                    }
                    //break;
                }
            }
        }
        return bRet;
    }

    // ISP

    public void setIspuCmdIndex(int index)
    {
        m_uCmdIndex = index;
    }

    public void setISPResendFlag(boolean val)
    {
        bResendFlag = val;
    }
    public boolean getISPResendFlag()
    {
        return bResendFlag;
    }

    int aaaRetry = 0;
    public boolean WriteData( long uCmd, byte[] pcBuffer, int dwLen, int dwMilliseconds)
    {
        if(DeviceConnection==null)
        {
            return false;
        }

        int dwCmdLength = dwLen;

        if(dwCmdLength>(ISPCmd_Con.MAX_BUFFER_SIZE-8))
            dwCmdLength = ISPCmd_Con.MAX_BUFFER_SIZE-8;

        for(int i=0;i<ISPCmd_Con.MAX_BUFFER_SIZE;i++)
        {
            DataBuf[i] = 0;
        }
        DataBuf[0] = (byte)(uCmd&0xFF);
        DataBuf[1] = (byte)((uCmd&0xFF00)>>8);
        DataBuf[2] = (byte)((uCmd&0xFF0000)>>8);
        DataBuf[3] = (byte)((uCmd&0xFF000000)>>8);
        DataBuf[4] = (byte)(m_uCmdIndex&0xFF);
        DataBuf[5] = (byte)((m_uCmdIndex&0xFF00)>>8);
        DataBuf[6] = (byte)((m_uCmdIndex&0xFF0000)>>8);
        DataBuf[7] = (byte)((m_uCmdIndex&0xFF000000)>>8);

        if((dwCmdLength>0)&&(pcBuffer!=null)) {
            System.arraycopy(pcBuffer,0,DataBuf,8,dwCmdLength);
        }
        m_usCheckSum = Checksum(DataBuf,dwCmdLength+8);

        int	 ret = DeviceConnection.bulkTransfer(epOut, DataBuf, ISPCmd_Con.MAX_BUFFER_SIZE, dwMilliseconds);

        if(ret==ISPCmd_Con.MAX_BUFFER_SIZE)
        {
            m_uCmdIndex += 2;
            return true;
        }
        return false;
    }

    public boolean ReadData(byte[] pcBuffer, int szMaxLen, int dwMilliseconds, boolean bCheckIndex)
    {
        bResendFlag = false;
        if(DeviceConnection==null)
        {
            return false;
        }

        int re = DeviceConnection.bulkTransfer(epIn, DataBuf, ISPCmd_Con.MAX_BUFFER_SIZE, dwMilliseconds);
        if(re  >= 8)
        {
            int usCheckSum =0;
            int uCmdIndex = 0;
            for(int a=0;a<4;a++)
            {
                usCheckSum +=  (int)((DataBuf[a]&0xff)<<(a*8));
                uCmdIndex +=  (int)((DataBuf[4+a]&0xff)<<(a*8));
            }
            if ((!bCheckIndex || uCmdIndex == m_uCmdIndex - 1)
                    && usCheckSum == m_usCheckSum) {
                if (szMaxLen > re - 8) {
                    szMaxLen = re - 8;
                }

                String str = "";
                for (int i = 0; i < re; i++) {
                    str = str +  Integer.toHexString(DataBuf[i])  + ", ";

                }
                //Log.d(TAG, "ReadData: " + str);
                System.arraycopy(DataBuf,8,pcBuffer,0,szMaxLen);
                return true;
            }
            else
            {
                Log.d(TAG, "ReadData: checksum or cmd index error! index=0x"+String.valueOf(uCmdIndex)+"="+String.valueOf(m_uCmdIndex)+" cks"+String.valueOf(usCheckSum)+"="+String.valueOf(m_usCheckSum) );
                bResendFlag = true;
                return false;
            }
        }
        bResendFlag = true;
        Log.d(TAG, "ReadData: read back length error! read size="+String.valueOf(re) );
        return false;
    }

    public void saveCoordinateTable(int phone,int game)
    {
        SharedPreferences.Editor editor = getSharedPreferences(Transfer_Con.SAVENAME, MODE_PRIVATE).edit();
        String strKey =String.format("coordinateP%02dG%02d",phone,game);

        String str="Pt";
        JSONArray jsonArray = new JSONArray();
        for (int data : ptTbl) {
            str+=String.valueOf(data)+", ";
            jsonArray.put(data);
        }
        editor.putString(strKey,jsonArray.toString());
        setIsPtExit(phone,game,true);
        Log.d(TAG,str);
        str="exist:";
        jsonArray = new JSONArray();
        for (int data : u8IsPtExist) {
            str+=String.valueOf(data)+", ";
            jsonArray.put(data);
        }
        editor.putString("ptExist",jsonArray.toString());
        Log.d(TAG,str);

        editor.commit();
    }
    public void loadCoordinateTable(int phone,int game)
    {
        SharedPreferences read = getSharedPreferences(Transfer_Con.SAVENAME, MODE_PRIVATE);
        String strKey =String.format("coordinateP%02dG%02d",phone,game);
        String str="";

        try {
            JSONArray jsonArray = new JSONArray(read.getString(strKey, "[]"));
            for (int i = 0; i < jsonArray.length(); i++) {
                ptTbl[i] = jsonArray.getInt(i);
                str += String.valueOf(ptTbl[i]) + ", ";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.d(TAG,str);
    }

    String get_Version_test(byte[] pReadBuf)
    {
        int mRet;
        String str;
            if(Transfer_Con.ENABLE_SUPPORT_FULL_CMD)
                DataBuf[0] = (byte)(Transfer_Con.HID_CMD_GET_VERSION);
            else
                DataBuf[0] = (byte)(0x80|Transfer_Con.HID_CMD_GET_VERSION);
            DataBuf[1] = 14;//
            IntToByte(0,DataBuf,2 );
            IntToByte(0,DataBuf,6 );
            IntToByte(Transfer_Con.HID_CMD_SIGNATURE,DataBuf,10 );
            int cks = Checksum(DataBuf, 14);
            IntToByte(cks,DataBuf,14 );
            int	 ret = DeviceConnection.bulkTransfer(epOut, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);
            if(ret!=Transfer_Con.HID_PACKET_SIZE) {
                Log.d(TAG, "Send command Fail!");
                return  "Send command Fail!";
            }

            while(true)
            {
                int re = DeviceConnection.bulkTransfer(epIn, DataBuf, Transfer_Con.HID_PACKET_SIZE, USBCMD_TIMEOUT);
                if(re<18)
                {
                    Log.d(TAG, "Get Data Fail! er="+String.valueOf(re));
                    return  "Get Data Fail! er="+String.valueOf(re);
                }
                else
                {
                    if((DataBuf[0]==Transfer_Con.HID_CMD_GET_VERSION)&& (DataBuf[2]!=0x00)) {
                        mRet = DataBuf[2];
                        System.arraycopy(DataBuf,18,pReadBuf,0,DataBuf[2]);
                        pReadBuf[DataBuf[2]]=0;
                        //for(int a=0;a<)
                        Log.d(TAG, "Get Version Success!");
                        byte[]data = new byte[mRet];
                        System.arraycopy(DataBuf,18,data,0,mRet);
                        str = "Get Version Success! ver = "+new String(data);
                    }
                    else {
                        mRet = 0;
                        Log.d(TAG, "Get Version fail!");
                        str = "Get Version fail!";
                    }
                    break;
                }
            }
        return str;
    }

    boolean writeFile(String fileName,String content)
    {
        boolean bRetVal = false;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

            String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
            File file = new File(sdPath + LOCAL_PATH);
            if (!file.exists()) {
                if (!file.mkdirs())//;//创建文件夹
                {
                    Log.d(TAG, "创建失败");
                    return false;
                }
            }
            String filePath = sdPath + LOCAL_PATH+"/" + fileName;
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(filePath);
                fos.write(content.getBytes(StandardCharsets.UTF_8));
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            bRetVal = true;
        }
        return bRetVal;
    }

    private void getFiles(ArrayList<File> list, String filePath) {
        File[] allFiles = new File(filePath).listFiles();
        if (allFiles != null) { // 若文件不为空，则遍历文件长度
            for (int i = 0; i < allFiles.length; i++) {
                File file = allFiles[i];
                if (file.isFile()) {
                    list.add(file);
                }
            }
        }
    }
    public ArrayList<String> getFileList(String extName) {
        ArrayList<String> fileList = new ArrayList<>();
        ArrayList<File> list = new ArrayList<File>();
        String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        String filePath = sdPath + LOCAL_PATH+"/";
        getFiles(list, filePath);
        for (int i = 0; i < list.size(); i++) {
            String mPicName = list.get(i).getName();
            String substring = mPicName.substring(mPicName.lastIndexOf(".") + 1);
            if(substring.equals(extName))
            {
                fileList.add(mPicName);
                Log.d(TAG,mPicName);
            }
        }
        return fileList;
    }


    String readFile(String fileName)
    {
        String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        String filePath = sdPath + LOCAL_PATH + "/" + fileName;
        FileInputStream fis = null;
        String content = null;
        try {
            fis = new FileInputStream(filePath);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = -1;
            while ((len = fis.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            content = baos.toString();
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content;
    }

    // Game Data on phone

    public void setDefaultGameData_KB(String pkgName, int[] posData,int [][] attrData, int [] attrEventTbl)
    {
        if(Transfer_Con.ENABLE_GAME_DATA_NEW_FILE)
        {
            if (pkgName.contains(Transfer_Con.defPackageName[1]))
            {
                // Coordinate
                System.arraycopy(Game_WZRY_Data.tCOORDINATE_WZRY_KB,0,posData,0,Game_WZRY_Data.tCOORDINATE_WZRY_KB.length);
                // Attribute
                for (int a = 0; a < Transfer_Con.MAX_COORDINATE; a++) {
                    attrData[a][0] = Game_WZRY_Data.tATTRIBUTE_WZRY_KB[a][0];
                    attrData[a][1] = Game_WZRY_Data.tATTRIBUTE_WZRY_KB[a][1];
                    attrData[a][2] = Game_WZRY_Data.tATTRIBUTE_WZRY_KB[a][2];
                }
                kbTbl[0] = (byte) 0xFF;
                kbTbl[1] = (byte) 0xFF;
                kbTbl[2] = (byte) 0xFF;
                kbTbl[3] = (byte) 0xFF;
                kbTbl[4] = (byte) 0xFF;
                // Macro
                System.arraycopy(Game_WZRY_Data.tMACRO_WZRY_KB,0,attrEventTbl,0,Game_WZRY_Data.tMACRO_WZRY_KB.length);
            }
            else
            {
                System.arraycopy(Game_HPJY_Data.tCOORDINATE_HPJY_KB,0,posData,0,Game_HPJY_Data.tCOORDINATE_HPJY_KB.length);
                for (int a = 0; a < Transfer_Con.MAX_COORDINATE; a++) {
                    attrData[a][0] = Game_HPJY_Data.tATTRIBUTE_HPJY_KB[a][0];
                    attrData[a][1] = Game_HPJY_Data.tATTRIBUTE_HPJY_KB[a][1];
                    attrData[a][2] = Game_HPJY_Data.tATTRIBUTE_HPJY_KB[a][2];
                }
                kbTbl[0] = 0x1a;
                kbTbl[1] = 0x16;
                kbTbl[2] = 0x04;
                kbTbl[3] = 0x07;
                kbTbl[4] = (byte) 0x82;
                // Macro
                System.arraycopy(Game_HPJY_Data.tMACRO_HPJY_KB,0,attrEventTbl,0,Game_HPJY_Data.tMACRO_HPJY_KB.length);
            }
        }
        else {
            if (pkgName.contains(Transfer_Con.defPackageName[1])) {
                int coordinate[] = {
                        32768, 32768,    //	0x00
                        32768, 32768,    //	0x01
                        32768, 32768,    //	0x02
                        32768, 32768,    // 	0x03
                        28105, 28065,    //普通攻击    //a	0x04
                        29387, 16455,    //公共技能1     //b
                        29633, 18590,    //公共技能2    // c
                        22829, 29000,   //推塔  // d
                        18983, 27945,    //英雄技能3    // e
                        29486, 20725,    //公共技能3    // f
                        14108, 23719,    //g
                        32768, 32768,    // h
                        32768, 32768,    // i
                        32768, 32768,    // j
                        32768, 32768,    // k
                        32768, 32768,    // l
                        32768, 32768,    // m
                        32768, 32768,    // n
                        32768, 32768,    // o
                        32768, 32768,    // p
                        28697, 23267,    //英雄技能1    // q
                        16271, 29576,    //英雄技能4    // r
                        29486, 25725,  //补刀  // s
                        32768, 32768,    // t
                        32768, 32768,    // u
                        14545, 1415,    //背包    // v
                        22829, 24971,    //英雄技能2    // w
                        3500, 29000,    //功能返回    // x
                        32768, 32768,    // y
                        25787, 6572,    //左摇杆    // z
                        25393, 21828,    //英雄技能点1    // 1
                        19279, 23507,    //英雄技能点2    // 2
                        15827, 26434,    //英雄技能点3    // 3
                        14743, 27993,    //英雄技能点4    // 4
                        13017, 3430,    //捡装备上    // 5
                        16715, 3382,    //捡装备下    // 6
                        32768, 32768,    // 7
                        32768, 32768,    // 8
                        32768, 32768,    // 9
                        32768, 32768,    // 0
                        32768, 32768,    // enter
                        32768, 32768,    // esc
                        32768, 32768,    // backspace
                        32768, 32768,    // tab
                        22985, 30416,    // space
                        32768, 32768,    // minus
                        32768, 32768,    // equal	// 0x2e
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,    // 0x39
                        32768, 32768,    // F1
                        32768, 32768,    // F2
                        32768, 32768,    // F3
                        32768, 32768,    // F4
                        32768, 32768,    // F5
                        32768, 32768,    // F6
                        32768, 32768,    // F7
                        32768, 32768,    // F8
                        32768, 32768,    // F9
                        32768, 32768,    // F10
                        32768, 32768,    // F11
                        32768, 32768,    // F12
                        32768, 32768,    // printf screen 0x46
                        32768, 32768,    // scroll lock
                        32768, 32768,    // pause
                        32768, 32768,    // insert
                        32768, 32768,    // home
                        32768, 32768,    // page up
                        32768, 32768,    // delete
                        32768, 32768,    // end
                        32768, 32768,    // page down
                        32768, 32768,    // right
                        32768, 32768,    // left
                        32768, 32768,    // down
                        32768, 32768,    // up
                        32768, 32768,    // 0x53
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        4141, 3478,    //地图    // 0x80 	left ctrl
                        32768, 32768,    // 0x81 	left shift
                        15975, 24995,    //右摇杆    // 0x82 	left alt
                        32768, 32768,    // 0x83 	left gui
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,    //左摇杆	Mouse
                        14108, 23719,    // 0x89	wheel
                        28105, 28065,    //普通攻击    // 0x8a	left button
                        25787, 6572,    // 0x8b 	right button
                        30578, 25019,    // 0x8c	mouse center
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        25787, 6572,    // 0x90	Direct center
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                };
                System.arraycopy(coordinate, 0, posData, 0, Transfer_Con.MAX_COORDINATE * 2);

                int attr[][] = {
                        {0xFF, 0xFFFF, 0xFFFF},
                        {0xFF, 0xFFFF, 0xFFFF},
                        {0xFF, 0xFFFF, 0xFFFF},
                        {0xFF, 0xFFFF, 0xFFFF},
                        {0x00, 0xFFFF, 0xFFFF},   // a    0x04
                        {0x00, 0xFFFF, 0xFFFF},   // b
                        {0x00, 0xFFFF, 0xFFFF},   // c
                        {0x00, 0xFFFF, 0xFFFF},   // d
                        {0x01, 5000, 0xFFFF},   // e
                        {0x00, 0xFFFF, 0xFFFF},   // f
                        {0x00, 0xFFFF, 0xFFFF},   // g
                        {0xFF, 0xFFFF, 0xFFFF},   // h
                        {0xFF, 0xFFFF, 0xFFFF},   // i
                        {0xFF, 0xFFFF, 0xFFFF},   // j
                        {0xFF, 0xFFFF, 0xFFFF},   // k
                        {0xFF, 0xFFFF, 0xFFFF},   // l
                        {0xFF, 0xFFFF, 0xFFFF},   // m
                        {0xFF, 0xFFFF, 0xFFFF},   // n
                        {0xFF, 0xFFFF, 0xFFFF},   // o
                        {0xFF, 0xFFFF, 0xFFFF},   // p
                        {0x01, 5000, 0xFFFF},   // q
                        {0x01, 5000, 0xFFFF},   // r
                        {0x00, 0xFFFF, 0xFFFF},   // s
                        {0xFF, 0xFFFF, 0xFFFF},   // t
                        {0xFF, 0xFFFF, 0xFFFF},   // u
                        {0x00, 0xFFFF, 0xFFFF},   // v
                        {0x01, 5000, 0xFFFF},   // w
                        {0x00, 0xFFFF, 0xFFFF},   // x
                        {0xFF, 0xFFFF, 0xFFFF},   // y
                        {0xFF, 0xFFFF, 0xFFFF},   // z
                        {0x00, 0xFFFF, 0xFFFF},   // 1
                        {0x00, 0xFFFF, 0xFFFF},   // 2
                        {0x00, 0xFFFF, 0xFFFF},   // 3
                        {0x00, 0xFFFF, 0xFFFF},   // 4
                        {0x00, 0xFFFF, 0xFFFF},   // 5
                        {0x00, 0xFFFF, 0xFFFF},   // 6
                        {0xFF, 0xFFFF, 0xFFFF},   // 7
                        {0xFF, 0xFFFF, 0xFFFF},   // 8
                        {0xFF, 0xFFFF, 0xFFFF},   // 9
                        {0xFF, 0xFFFF, 0xFFFF},   // 0
                        {0xFF, 0xFFFF, 0xFFFF},    // enter
                        {0xFF, 0xFFFF, 0xFFFF},    // esc
                        {0xFF, 0xFFFF, 0xFFFF},    // backspace
                        {0xFF, 0xFFFF, 0xFFFF},    // tab
                        {0xFF, 0xFFFF, 0xFFFF},    // space
                        {0xFF, 0xFFFF, 0xFFFF},    // minus
                        {0xFF, 0xFFFF, 0xFFFF},    // equal	// 0x2e
                        {0xFF, 0xFFFF, 0xFFFF},               // 0x2f
                        {0xFF, 0xFFFF, 0xFFFF},               // 0x30
                        {0xFF, 0xFFFF, 0xFFFF},               // 0x31
                        {0xFF, 0xFFFF, 0xFFFF},               // 0x32
                        {0xFF, 0xFFFF, 0xFFFF},               // 0x33
                        {0xFF, 0xFFFF, 0xFFFF},               // 0x34
                        {0xFF, 0xFFFF, 0xFFFF},               // 0x35
                        {0xFF, 0xFFFF, 0xFFFF},               // 0x36
                        {0xFF, 0xFFFF, 0xFFFF},               // 0x37
                        {0xFF, 0xFFFF, 0xFFFF},               // 0x38
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x39
                        {0xFF, 0xFFFF, 0xFFFF},   // F1
                        {0xFF, 0xFFFF, 0xFFFF},   // F2
                        {0xFF, 0xFFFF, 0xFFFF},    // F3
                        {0xFF, 0xFFFF, 0xFFFF},   // F4
                        {0xFF, 0xFFFF, 0xFFFF},    // F5
                        {0xFF, 0xFFFF, 0xFFFF},   // F6
                        {0xFF, 0xFFFF, 0xFFFF},   // F7
                        {0xFF, 0xFFFF, 0xFFFF},    // F8
                        {0xFF, 0xFFFF, 0xFFFF},   // F9
                        {0xFF, 0xFFFF, 0xFFFF},   // F10
                        {0xFF, 0xFFFF, 0xFFFF},    // F11
                        {0xFF, 0xFFFF, 0xFFFF},    // F12
                        {0xFF, 0xFFFF, 0xFFFF},   // printf screen 0x46
                        {0xFF, 0xFFFF, 0xFFFF},   // scroll lock
                        {0xFF, 0xFFFF, 0xFFFF},    // pause
                        {0xFF, 0xFFFF, 0xFFFF},    // insert
                        {0xFF, 0xFFFF, 0xFFFF},    // home
                        {0xFF, 0xFFFF, 0xFFFF},    // page up
                        {0xFF, 0xFFFF, 0xFFFF},  // delete
                        {0xFF, 0xFFFF, 0xFFFF},    // end
                        {0xFF, 0xFFFF, 0xFFFF},    // page down
                        {0xFF, 0xFFFF, 0xFFFF},    // right
                        {0xFF, 0xFFFF, 0xFFFF},    // left
                        {0xFF, 0xFFFF, 0xFFFF},    // down
                        {0xFF, 0xFFFF, 0xFFFF},   // up
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x53
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x54
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x55
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x56
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x57
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x58
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x59
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x5a
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x5b
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x5c
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x5d
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x5e
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x5f
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x60
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x61
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x62
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x63
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x64
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x65
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x66
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x67
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x68
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x69
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x6a
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x6b
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x6c
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x6d
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x6e
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x6f
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x70
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x71
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x72
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x73
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x74
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x75
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x76
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x77
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x78
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x79
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x7a
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x7b
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x7c
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x7d
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x7e
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x7f
                        {0x01, 5000, 0xFFFF},     // 0x80 	left ctrl
                        {0xFF, 0xFFFF, 0xFFFF},    // 0x81 	left shift
                        {0x01, 0xFFFF, 0xFFFF},    // 0x82 	left alt
                        {0xFF, 0xFFFF, 0xFFFF},    // 0x83 	left gui
                        {0xFF, 0xFFFF, 0xFFFF},   //  0x84
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x85
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x86
                        {0xFF, 0xFFFF, 0xFFFF},   // 0x87
                        {0xFF, 0xFFFF, 0xFFFF},    // 0x88	Mouse
                        {0xFF, 0xFFFF, 0xFFFF},    // 0x89	wheel
                        {0x00, 0xFFFF, 0xFFFF},    // 0x8a	left button
                        {0x01, 0xFFFF, 0xFFFF},    // 0x8b 	right button
                        {0xFF, 0xFFFF, 0xFFFF},    // 0x8c	mouse center
                        {0xFF, 0xFFFF, 0xFFFF},
                        {0xFF, 0xFFFF, 0xFFFF},
                        {0xFF, 0xFFFF, 0xFFFF},
                        {0xFF, 0xFFFF, 0xFFFF},    // 0x90	Direct center
                        {0xFF, 0xFFFF, 0xFFFF},
                        {0xFF, 0xFFFF, 0xFFFF},
                        {0xFF, 0xFFFF, 0xFFFF},
                        {0xFF, 0xFFFF, 0xFFFF},
                        {0xFF, 0xFFFF, 0xFFFF},
                        {0xFF, 0xFFFF, 0xFFFF},
                        {0xFF, 0xFFFF, 0xFFFF},
                        {0xFF, 0xFFFF, 0xFFFF},
                        {0xFF, 0xFFFF, 0xFFFF},
                        {0xFF, 0xFFFF, 0xFFFF},
                        {0xFF, 0xFFFF, 0xFFFF},
                        {0xFF, 0xFFFF, 0xFFFF},
                        {0xFF, 0xFFFF, 0xFFFF},
                        {0xFF, 0xFFFF, 0xFFFF},
                        {0xFF, 0xFFFF, 0xFFFF},
                };


                for (int a = 0; a < Transfer_Con.MAX_COORDINATE; a++) {

                    attrData[a][0] = attr[a][0];
                    attrData[a][1] = attr[a][1];
                    attrData[a][2] = attr[a][2];
                }

                kbTbl[0] = (byte) 0xFF;
                kbTbl[1] = (byte) 0xFF;
                kbTbl[2] = (byte) 0xFF;
                kbTbl[3] = (byte) 0xFF;
                kbTbl[4] = (byte) 0xFF;


            } else {
                int coordinate[] = {
                        32768, 32768,    //	0x00
                        32768, 32768,    //	0x01
                        32768, 32768,    //	0x02
                        32768, 32768,    // 	0x03
                        24575, 6036,    //a	0x04
                        29963, 3094,    // b
                        30270, 21420,    // c
                        24575, 6036,    // d
                        11711, 6996,    // e
                        11316, 23674,    // f
                        14108, 23719,    //g
                        17475, 23674,    // h
                        32768, 32768,    // i
                        12742, 29460,    // j
                        32768, 32768,    // k
                        32768, 32768,    // l
                        1693, 30704,    // m
                        32768, 32768,    // n
                        32768, 32768,    // o
                        32768, 32768,    // p
                        11650, 4619,    // q
                        30578, 25019,    // r
                        24575, 6036,    // s
                        8222, 22700,    // t
                        32768, 32768,    // u
                        30885, 7247,    // v
                        24575, 6036,    // w
                        1693, 30704,    // x
                        8252, 23911,    // y
                        32768, 32768,    // z
                        29035, 14538,    // 1       // 0x1E
                        29126, 18730,    // 2       // 0x1F
                        26881, 19660,    // 3
                        20024, 16383,    // 4
                        32768, 32768,    // 5
                        32768, 32768,    // 6
                        32768, 32768,    // 7
                        32768, 32768,    // 8
                        30612, 10892,    // 9
                        14290, 8265,    // 0
                        32768, 32768,    // enter
                        32768, 32768,    // esc
                        32768, 32768,    // backspace
                        32768, 32768,    // tab
                        22985, 30416,    // space
                        32768, 32768,    // minus
                        11074, 3114,    // equal	// 0x2e
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,    // 0x39
                        8586, 27379,    // F1
                        32768, 32768,    // F2
                        32768, 32768,    // F3
                        32768, 32768,    // F4
                        32768, 32768,    // F5
                        32768, 32768,    // F6
                        32768, 32768,    // F7
                        32768, 32768,    // F8
                        32768, 32768,    // F9
                        32768, 32768,    // F10
                        32768, 32768,    // F11
                        32768, 32768,    // F12
                        32768, 32768,    // printf screen 0x46
                        32768, 32768,    // scroll lock
                        32768, 32768,    // pause
                        32768, 32768,    // insert
                        32768, 32768,    // home
                        32768, 32768,    // page up
                        32768, 32768,    // delete
                        32768, 32768,    // end
                        32768, 32768,    // page down
                        21935, 27232,    // right
                        22876, 22730,    // left
                        25667, 5402,    // down
                        17263, 5372,    // up
                        32768, 32768,    // 0x53
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        29501, 29960,    // 0x80 	left ctrl
                        30424, 27345,    // 0x81 	left shift
                        10194, 25711,    // 0x82 	left alt
                        32768, 32768,    // 0x83 	left gui
                        29501, 29960,
                        30424, 27345,
                        32768, 32768,
                        32768, 32768,
                        11407, 19069,    // 0x88	Mouse
                        14108, 23719,    // 0x89	wheel
                        24575, 27571,    // 0x8a	left button
                        17172, 30907,    // 0x8b 	right button
                        30578, 25019,    // 0x8c	mouse center
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        24575, 6036,    // 0x90	Direct center
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                        32768, 32768,
                };
                System.arraycopy(coordinate, 0, posData, 0, Transfer_Con.MAX_COORDINATE * 2);
                for (int a = 0; a < Transfer_Con.MAX_COORDINATE; a++) {
                    for (int i = 0; i < 3; i++) {
                        if ((coordinate[a * 2] == 0x8000) || (coordinate[a * 2 + 1] == 0x8000))
                            attrData[a][i] = 0xFFFF;
                        else
                            attrData[a][i] = 0x0000;
                    }
                }

                attrData[0x1A][0] = 0x20;   // W
                attrData[0x16][0] = 0x21;   // S
                attrData[0x04][0] = 0x22;   // A
                attrData[0x07][0] = 0x23;   // D
                attrData[0x82][0] = 0x24;   // L ALT

                kbTbl[0] = 0x1a;
                kbTbl[1] = 0x16;
                kbTbl[2] = 0x04;
                kbTbl[3] = 0x07;
                kbTbl[4] = (byte) 0x82;
            }

            for (int a = 0; a < Transfer_Con.MAX_AUTOEVENT_LENGTH_KB; a++)
                attrEventTbl[a] = 0;



        }
        localSetting_KB = new int[100];

        localSetting_KB[0] = 5;    // mouse h
        localSetting_KB[1] = 5;    // mouse v
        localSetting_KB[2] =  0x01; // quick switch mouse group
        localSetting_KB[3] =  0x29; // quick switch mouse key
    }

    public boolean loadGameData_KB(String pkgName,String labelName,int[] posData,int [][] attrData,int [] attrEventData)
    {
        int ver = 0;
        SharedPreferences read = getSharedPreferences(Transfer_Con.SAVEGAMEDATA, MODE_PRIVATE);
        if(read==null)
        {
            read = getSharedPreferences(Transfer_Con.SAVEGAMEDATA, MODE_PRIVATE);
            if(read==null)
            {
                return  false;
            }
        }

        String strKeyName = getDeviceName()+"_"+pkgName;

        String content = read.getString(strKeyName, "");
        String deviceName,loadName,loadLabel;
        if((content==null)||(content.equals("")))
        {
            return  false;
        }
        try {
            JSONArray jsonArray = new JSONArray(content);
            //Log.d(TAG,"loadGameData_KB: "+content);
            if(jsonArray.length()<=0)
                return false;
            deviceName = jsonArray.getString(0);
            loadName = jsonArray.getString(1);
            loadLabel = jsonArray.getString(2);
            if(deviceName.equals(getDeviceName()))
            {
                ver = 100;
            }
            else if(deviceName.contains(getDeviceName()))
            {
               String []st =  deviceName.split("_");
                ver = Integer.parseInt(st[1]);

            }
            if((!loadName.equals(pkgName))||(!deviceName.contains(getDeviceName()))||(!labelName.equals(loadLabel)))
                return false;
            for(int a=0;a<Transfer_Con.MAX_COORDINATE*2;a++)    // coordinate
            {
                posData[a] = jsonArray.getInt(3+a);
            }
            for(int a=0;a<Transfer_Con.MAX_COORDINATE;a++)      // Key Attribute
            {
                for(int i=0;i<3;i++)
                {
                    attrData[a][i] = jsonArray.getInt(3+Transfer_Con.MAX_COORDINATE*2+a*3+i);
                }
            }
            // Attribute event table
            if(jsonArray.length()>=(3+Transfer_Con.MAX_COORDINATE*5+Transfer_Con.MAX_AUTOEVENT_LENGTH_KB))
            {
                for(int a=0;a<Transfer_Con.MAX_AUTOEVENT_LENGTH_KB;a++)    // coordinate
                {
                    attrEventData[a] = jsonArray.getInt(3+Transfer_Con.MAX_COORDINATE*5+a);
                }
            }
            else
            {
                for(int a=0;a<Transfer_Con.MAX_AUTOEVENT_LENGTH_KB;a++)    // coordinate
                {
                    attrEventData[a] = 0;
                }
            }
            if(ver<101) // Transfer_Con.GAME_SAVE_DATA_VER
            {
                int temp_x = posData[0x8D*2];
                int temp_y = posData[0x8D*2+1];
                posData[0x8D*2] = 32768;
                posData[0x8D*2+1] = 32768;

                posData[0x90*2] = temp_x;
                posData[0x90*2+1] = temp_y;

                attrData[0x8D][0] = 0xFF;
                attrData[0x8D][1] = 0xFFFF;
                attrData[0x8D][2] = 0xFFFF;

                attrData[0x90][0] = 0x00;
                attrData[0x90][1] = 0xFFFF;
                attrData[0x90][2] = 0xFFFF;

                localSetting_KB[0] = 5;
                localSetting_KB[1] = 5;
                localSetting_KB[2] = 0x01;
                localSetting_KB[3] = 0x29;


            }
            else
            {
                for(int a=0;a<100;a++)    // local setting
                {
                    localSetting_KB[a] = jsonArray.getInt(3+Transfer_Con.MAX_COORDINATE*5+Transfer_Con.MAX_AUTOEVENT_LENGTH_KB+a);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }



        for(int a=0;a<0x90;a++)
        {
            if(attrData[a][0] ==0x20)
            {
                kbTbl[0] = (byte)a;
            }
            else if(attrData[a][0] ==0x21)
            {
                kbTbl[1] = (byte)a;
            }
            else if(attrData[a][0] ==0x22)
            {
                kbTbl[2] = (byte)a;
            }
            else if(attrData[a][0] ==0x23)
            {
                kbTbl[3] = (byte)a;
            }
            else if(attrData[a][0] ==0x24)
            {
                kbTbl[4] = (byte)a;
            }
        }
        return true;
    }
    public void saveGameData_KB(String pkgName,String labelName,int[] posData,int [][] attrData,int [] attrEventData)
    {
        String strKeyName = getDeviceName()+"_"+pkgName;
        SharedPreferences.Editor editor = getSharedPreferences(Transfer_Con.SAVEGAMEDATA, MODE_PRIVATE).edit();
        JSONArray jsonArray = new JSONArray();
        String  str = getDeviceName() + "_"+String.valueOf(Transfer_Con.GAME_SAVE_DATA_VER);
        jsonArray.put(str);
        jsonArray.put(pkgName);
        jsonArray.put(labelName);
        for (int data : posData) {
            jsonArray.put(data);
        }
        for(int [] array : attrData) {
            for (int data : array) {
                jsonArray.put(data);
            }
        }

        for (int data : attrEventData) {
            jsonArray.put(data);
        }

        // local setting

        for (int data : localSetting_KB) {
            jsonArray.put(data);
        }

        String content = jsonArray.toString();
        editor.putString(strKeyName,content);
        Log.d(TAG,"saveGameData_KB: "+content);
        editor.apply();
    }

    public void clearGameData_KB()
    {
        SharedPreferences.Editor editor = getSharedPreferences(Transfer_Con.SAVEGAMEDATA, MODE_PRIVATE).edit();
        editor.clear();
        editor.commit();
    }

    // sKey = device name + "_" + package name
    public void deleteGameData_KB()
    {
        SharedPreferences.Editor editor = getSharedPreferences(Transfer_Con.SAVEGAMEDATA, MODE_PRIVATE).edit();
        editor.clear();
        editor.commit();
    }

    //   game data for joy stick or game pad or game handle

    public boolean loadGameData_GP(String pkgName,String labelName,int[] posData,int [][] attrData,int [] attrEventData)
    {
        SharedPreferences read = getSharedPreferences(Transfer_Con.SAVEGAMEDATA, MODE_PRIVATE);
        if(read==null)
        {
            read = getSharedPreferences(Transfer_Con.SAVEGAMEDATA, MODE_PRIVATE);
            if(read==null)
            {
                return  false;
            }
        }
        String strKeyName = getDeviceName()+"_"+pkgName;

        String content = read.getString(strKeyName, "");
        String deviceName,loadName,loadLabel;
        if((content==null)||(content.equals("")))
        {
            return  false;
        }
        try {
            JSONArray jsonArray = new JSONArray(content);
            Log.d(TAG,"loadGameData_GP: "+content);
            if(jsonArray.length()<=0)
                return false;
            deviceName = jsonArray.getString(0);
            loadName = jsonArray.getString(1);
            loadLabel = jsonArray.getString(2);
            if((!loadName.equals(pkgName))||(!deviceName.equals(getDeviceName()))||(!labelName.equals(loadLabel)))
                return false;
            for(int a=0;a<(getDeviceMaxButton()*2)*5;a++)
            {
                posData[a] = jsonArray.getInt(3+a);
            }
            for(int a=0;a<getDeviceMaxButton();a++)
            {
                for(int i=0;i<3;i++)
                {
                    attrData[a][i] = jsonArray.getInt(323+a*3+i);
                }
            }
            for(int a=0;a<Transfer_Con.MAX_AUTOEVENT_LENGTH_KB;a++)
            {
                attrEventData[a] = jsonArray.getInt(323+480+a);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return true;
    }
    public void saveGameData_GP(String pkgName,String labelName,int[] posData,int [][] attrData,int [] attrEventData)
    {
        String strKeyName = getDeviceName()+"_"+pkgName;
        SharedPreferences.Editor editor = getSharedPreferences(Transfer_Con.SAVEGAMEDATA, MODE_PRIVATE).edit();
        JSONArray jsonArray = new JSONArray();
        jsonArray.put(getDeviceName());
        jsonArray.put(pkgName);
        jsonArray.put(labelName);

        for (int a=0;a<Transfer_Con.MAX_COORDINATE*2;a++) {
            jsonArray.put(posData[a]);
        }

        for(int a=0;a<Transfer_Con.MAX_COORDINATE;a++) {

            for (int k=0;k<3;k++) {
                jsonArray.put(attrTbl[a][k]);
            }
        }
        for (int a=0;a<Transfer_Con.MAX_AUTOEVENT_LENGTH_KB;a++) {
            jsonArray.put(attrEvTbl[a]);
        }

        String content = jsonArray.toString();
        editor.putString(strKeyName,content);
        Log.d(TAG,"saveGameData_GP: "+content);
        Toast.makeText(this, getResources().getString(R.string.string_pop_file_save_success), Toast.LENGTH_SHORT).show();
        editor.apply();
    }

    public void setDefaultGameData_GP(String pkgName, int[] posData,int [][] attrData,int [] attrEventData)
    {
        if(pkgName.contains(Transfer_Con.defPackageName[1]))
        {
            if(getDeviceIndex()==Transfer_Con.DEVICE_GM1_INDEX) {
                System.arraycopy(Game_WZRY_Data.tCOORDINATE_WZYR_GP_M, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_WZRY_Data.tATTRIBUTE_WZRY_GP_M[a][0];
                    attrData[a][1] = Game_WZRY_Data.tATTRIBUTE_WZRY_GP_M[a][1];
                    attrData[a][2] = Game_WZRY_Data.tATTRIBUTE_WZRY_GP_M[a][2];
                }
            }
            else {

                System.arraycopy(Game_WZRY_Data.tCOORDINATE_WZYR_GP_N, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_WZRY_Data.tATTRIBUTE_WZRY_GP_N[a][0];
                    attrData[a][1] = Game_WZRY_Data.tATTRIBUTE_WZRY_GP_N[a][1];
                    attrData[a][2] = Game_WZRY_Data.tATTRIBUTE_WZRY_GP_N[a][2];
                }
            }
            System.arraycopy(Game_WZRY_Data.tMACRO_WZRY_GP,0,attrEventData,0,Game_WZRY_Data.tMACRO_WZRY_GP.length);
            // 一键复活甲
        }
        else if(pkgName.contains(Transfer_Con.defPackageName[3]))
        {
            if(getDeviceIndex()==Transfer_Con.DEVICE_GM1_INDEX) {
                System.arraycopy(Game_QQFC_Data.tCOORDINATE_QQFC_GP_M, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_QQFC_Data.tATTRIBUTE_QQFC_GP_M[a][0];
                    attrData[a][1] = Game_QQFC_Data.tATTRIBUTE_QQFC_GP_M[a][1];
                    attrData[a][2] = Game_QQFC_Data.tATTRIBUTE_QQFC_GP_M[a][2];
                }
            }
            else {
                System.arraycopy(Game_QQFC_Data.tCOORDINATE_QQFC_GP_N, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_QQFC_Data.tATTRIBUTE_QQFC_GP_N[a][0];
                    attrData[a][1] = Game_QQFC_Data.tATTRIBUTE_QQFC_GP_N[a][1];
                    attrData[a][2] = Game_QQFC_Data.tATTRIBUTE_QQFC_GP_N[a][2];
                }
            }
            //System.arraycopy(Game_QQFC_Data.tMACRO_QQFC_GP,0,attrEventData,0,Game_QQFC_Data.tMACRO_QQFC_GP.length);
        }
        else if(pkgName.contains(Transfer_Con.defPackageName[4]))
        {
            if(getDeviceIndex()==Transfer_Con.DEVICE_GM1_INDEX) {
                System.arraycopy(Game_HYXD_Data.tCOORDINATE_HYXD_GP_M, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_HYXD_Data.tATTRIBUTE_HYXD_GP_M[a][0];
                    attrData[a][1] = Game_HYXD_Data.tATTRIBUTE_HYXD_GP_M[a][1];
                    attrData[a][2] = Game_HYXD_Data.tATTRIBUTE_HYXD_GP_M[a][2];
                }
            }
            else {
                System.arraycopy(Game_HYXD_Data.tCOORDINATE_HYXD_GP_N, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_HYXD_Data.tATTRIBUTE_HYXD_GP_N[a][0];
                    attrData[a][1] = Game_HYXD_Data.tATTRIBUTE_HYXD_GP_N[a][1];
                    attrData[a][2] = Game_HYXD_Data.tATTRIBUTE_HYXD_GP_N[a][2];
                }
            }
        }
        else if(pkgName.contains(Transfer_Con.defPackageName[5]))
        {
            if(getDeviceIndex()==Transfer_Con.DEVICE_GM1_INDEX) {
                System.arraycopy(Game_MRZH_Data.tCOORDINATE_MRZH_GP_M, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_MRZH_Data.tATTRIBUTE_MRZH_GP_M[a][0];
                    attrData[a][1] = Game_MRZH_Data.tATTRIBUTE_MRZH_GP_M[a][1];
                    attrData[a][2] = Game_MRZH_Data.tATTRIBUTE_MRZH_GP_M[a][2];
                }
            }
            else {
                System.arraycopy(Game_MRZH_Data.tCOORDINATE_MRZH_GP_N, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_MRZH_Data.tATTRIBUTE_MRZH_GP_N[a][0];
                    attrData[a][1] = Game_MRZH_Data.tATTRIBUTE_MRZH_GP_N[a][1];
                    attrData[a][2] = Game_MRZH_Data.tATTRIBUTE_MRZH_GP_N[a][2];
                }
            }
        }
        else if(pkgName.contains(Transfer_Con.defPackageName[6]))
        {
            if(getDeviceIndex()==Transfer_Con.DEVICE_GM1_INDEX) {
                System.arraycopy(Game_GRSM_Data.tCOORDINATE_GRSM_GP_M, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_GRSM_Data.tATTRIBUTE_GRSM_GP_M[a][0];
                    attrData[a][1] = Game_GRSM_Data.tATTRIBUTE_GRSM_GP_M[a][1];
                    attrData[a][2] = Game_GRSM_Data.tATTRIBUTE_GRSM_GP_M[a][2];
                }
            }
            else {
                System.arraycopy(Game_GRSM_Data.tCOORDINATE_GRSM_GP_N, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_GRSM_Data.tATTRIBUTE_GRSM_GP_N[a][0];
                    attrData[a][1] = Game_GRSM_Data.tATTRIBUTE_GRSM_GP_N[a][1];
                    attrData[a][2] = Game_GRSM_Data.tATTRIBUTE_GRSM_GP_N[a][2];
                }
            }
        }
        else if(pkgName.contains(Transfer_Con.defPackageName[7]))
        {
            if(getDeviceIndex()==Transfer_Con.DEVICE_GM1_INDEX) {
                System.arraycopy(Game_WPZZ_Data.tCOORDINATE_WPZZ_GP_M, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_WPZZ_Data.tATTRIBUTE_WPZZ_GP_M[a][0];
                    attrData[a][1] = Game_WPZZ_Data.tATTRIBUTE_WPZZ_GP_M[a][1];
                    attrData[a][2] = Game_WPZZ_Data.tATTRIBUTE_WPZZ_GP_M[a][2];
                }
            }
            else {
                System.arraycopy(Game_WPZZ_Data.tCOORDINATE_WPZZ_GP_N, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_WPZZ_Data.tATTRIBUTE_WPZZ_GP_N[a][0];
                    attrData[a][1] = Game_WPZZ_Data.tATTRIBUTE_WPZZ_GP_N[a][1];
                    attrData[a][2] = Game_WPZZ_Data.tATTRIBUTE_WPZZ_GP_N[a][2];
                }
            }
        }
        else if(pkgName.contains(Transfer_Con.defPackageName[8]))
        {
            if(getDeviceIndex()==Transfer_Con.DEVICE_GM1_INDEX) {
                System.arraycopy(Game_SPR_Data.tCOORDINATE_SPR_GP_M, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_SPR_Data.tATTRIBUTE_SPR_GP_M[a][0];
                    attrData[a][1] = Game_SPR_Data.tATTRIBUTE_SPR_GP_M[a][1];
                    attrData[a][2] = Game_SPR_Data.tATTRIBUTE_SPR_GP_M[a][2];
                }
            }
            else {
                System.arraycopy(Game_SPR_Data.tCOORDINATE_SPR_GP_N, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_SPR_Data.tATTRIBUTE_SPR_GP_N[a][0];
                    attrData[a][1] = Game_SPR_Data.tATTRIBUTE_SPR_GP_N[a][1];
                    attrData[a][2] = Game_SPR_Data.tATTRIBUTE_SPR_GP_N[a][2];
                }
            }
        }
        else if(pkgName.contains(Transfer_Con.defPackageName[9]))
        {
            if(getDeviceIndex()==Transfer_Con.DEVICE_GM1_INDEX) {
                System.arraycopy(Game_CYHX_Data.tCOORDINATE_CYHX_GP_M, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_CYHX_Data.tATTRIBUTE_CYHX_GP_M[a][0];
                    attrData[a][1] = Game_CYHX_Data.tATTRIBUTE_CYHX_GP_M[a][1];
                    attrData[a][2] = Game_CYHX_Data.tATTRIBUTE_CYHX_GP_M[a][2];
                }
            }
            else {
                System.arraycopy(Game_CYHX_Data.tCOORDINATE_CYHX_GP_N, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_CYHX_Data.tATTRIBUTE_CYHX_GP_N[a][0];
                    attrData[a][1] = Game_CYHX_Data.tATTRIBUTE_CYHX_GP_N[a][1];
                    attrData[a][2] = Game_CYHX_Data.tATTRIBUTE_CYHX_GP_N[a][2];
                }
            }
        }
        else if(pkgName.contains(Transfer_Con.defPackageName[10]))
        {
            if(getDeviceIndex()==Transfer_Con.DEVICE_GM1_INDEX) {
                System.arraycopy(Game_JL_Data.tCOORDINATE_JL_GP_M, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_JL_Data.tATTRIBUTE_JL_GP_M[a][0];
                    attrData[a][1] = Game_JL_Data.tATTRIBUTE_JL_GP_M[a][1];
                    attrData[a][2] = Game_JL_Data.tATTRIBUTE_JL_GP_M[a][2];
                }
            }
            else {
                System.arraycopy(Game_JL_Data.tCOORDINATE_JL_GP_N, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_JL_Data.tATTRIBUTE_JL_GP_N[a][0];
                    attrData[a][1] = Game_JL_Data.tATTRIBUTE_JL_GP_N[a][1];
                    attrData[a][2] = Game_JL_Data.tATTRIBUTE_JL_GP_N[a][2];
                }
            }
        }
        else if (pkgName.contains(Transfer_Con.defPackageName[0]))
        {
            if(getDeviceIndex()==Transfer_Con.DEVICE_GM1_INDEX) {
                System.arraycopy(Game_HPJY_Data.tCOORDINATE_HPJY_GP_M, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_HPJY_Data.tATTRIBUTE_HPJY_GP_M[a][0];
                    attrData[a][1] = Game_HPJY_Data.tATTRIBUTE_HPJY_GP_M[a][1];
                    attrData[a][2] = Game_HPJY_Data.tATTRIBUTE_HPJY_GP_M[a][2];
                }
            }
            else {
                System.arraycopy(Game_HPJY_Data.tCOORDINATE_HPJY_GP_N, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_HPJY_Data.tATTRIBUTE_HPJY_GP_N[a][0];
                    attrData[a][1] = Game_HPJY_Data.tATTRIBUTE_HPJY_GP_N[a][1];
                    attrData[a][2] = Game_HPJY_Data.tATTRIBUTE_HPJY_GP_N[a][2];
                }
            }
            System.arraycopy(Game_HPJY_Data.tMACRO_HPJY_GP,0,attrEventData,0,Game_HPJY_Data.tMACRO_HPJY_GP.length);
        }
        else
        {
            if(getDeviceIndex()==Transfer_Con.DEVICE_GM1_INDEX) {
                System.arraycopy(Game_EMPTY_Data.tCOORDINATE_EMPTY_GP_M, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_EMPTY_Data.tATTRIBUTE_EMPTY_GP_M[a][0];
                    attrData[a][1] = Game_EMPTY_Data.tATTRIBUTE_EMPTY_GP_M[a][1];
                    attrData[a][2] = Game_EMPTY_Data.tATTRIBUTE_EMPTY_GP_M[a][2];
                }
            }
            else {
                System.arraycopy(Game_EMPTY_Data.tCOORDINATE_EMPTY_GP_N, 0, posData, 0, getDeviceMaxButton() * 2);
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    attrData[a][0] = Game_EMPTY_Data.tATTRIBUTE_EMPTY_GP_N[a][0];
                    attrData[a][1] = Game_EMPTY_Data.tATTRIBUTE_EMPTY_GP_N[a][1];
                    attrData[a][2] = Game_EMPTY_Data.tATTRIBUTE_EMPTY_GP_N[a][2];
                }
            }
            //System.arraycopy(Game_HPJY_Data.tMACRO_HPJY_GP,0,attrEventData,0,Game_HPJY_Data.tMACRO_HPJY_GP.length);
        }
        // combo
        for(int k=0;k<4;k++)
        {
            for(int a=0;a<getDeviceMaxButton();a++)
            {
                posData[(1+k)*getDeviceMaxButton()*2+a*2] = 0x7FFF;
                posData[(1+k)*getDeviceMaxButton()*2+a*2+1] = 0x7FFF;
            }
        }


    }

    // Game data exchange with device

    public void sendCoordinateToDevice_KB()
    {
        byte length = 20;
        int retry=0;
        byte []tempData = new byte[Transfer_Con.HID_PACKET_SIZE];
        if(getbIsUsbConnect())
        {
            for(int a=0;a<Transfer_Con.MAX_COORDINATE*2;)
            {
                for(int k=0;k<length;k++)
                {
                    tempData[k * 2] = (byte) (coorTbl[a+k] & 0xff);
                    tempData[k * 2 + 1] = (byte) (coorTbl[a+k] >> 8);
                }
                for(retry=0;retry<5;retry++) {
                    if (tranSet_MultiValue(Transfer_Con.HID_CMD_SET_POINT_TBL, a, length * 2, tempData)) {
                        a += length;
                        if ((a + length) > Transfer_Con.MAX_COORDINATE * 2)
                            length = (byte) (Transfer_Con.MAX_COORDINATE * 2 - a);
                        break;
                    }
                }
                if(retry>=5)
                {
                    Toast.makeText(getApplicationContext(),"sendCoordinateToDevice_KB USB ERROR",Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        }
    }

    public void sendCoordinateToDevice_GP(boolean bIsSave)
    {
        byte []tempData = new byte[Transfer_Con.HID_PACKET_SIZE];
        for (int a = 0; a < getDeviceMaxButton(); a++) {
            tempData[a * 2] = (byte) (coorTbl[a] & 0xff);
            tempData[a * 2 + 1] = (byte) (coorTbl[a] >> 8);
        }
        String str="";
        for (int a = 0; a < getDeviceMaxButton(); a++) {
            str+=" "+String.valueOf(coorTbl[a])+",";
        }
        //Log.d(TAG,"-----------------------------44444"+str);
        if (set_CurPtTbl((byte) (getDeviceMaxButton() * 2), (byte) 0, tempData)) {
            for (int a = 0; a < getDeviceMaxButton(); a++) {
                tempData[a * 2] = (byte) (coorTbl[getDeviceMaxButton() + a] & 0xff);
                tempData[a * 2 + 1] = (byte) (coorTbl[getDeviceMaxButton() + a] >> 8);
            }

            if (set_CurPtTbl((byte) (getDeviceMaxButton() * 2), (byte) 1, tempData)) {
                if(bIsSave) {
                    if (tranSet_Value(Transfer_Con.HID_CMD_SAVE_POINT_TBL, getmCurPhone(), Math.min(getmCurGame(), Transfer_Con.MAX_GP_FIX_GAME_NUM))) {
                        if (BuildConfig.DEBUG)
                            Toast.makeText(this, getResources().getString(R.string.string_pop_file_save_success), Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }

        for (int k = 0; k < 4; k++) {
                for (int a = 0; a < getDeviceMaxButton(); a++) {
                    tempData[a * 2] = (byte) (coorTbl[getDeviceMaxButton()*(1+k)*2+a] & 0xff);
                    tempData[a * 2 + 1] = (byte) (coorTbl[getDeviceMaxButton()*(1+k)*2+a] >> 8);
                }
                if (tranSetComboTbl((byte) k, (byte) (getDeviceMaxButton() * 2), (byte) 0, tempData)) {

                    for (int a = 0; a < getDeviceMaxButton(); a++) {
                        tempData[a * 2] = (byte) (coorTbl[getDeviceMaxButton()*(1+k)*2+getDeviceMaxButton()+a] & 0xff);
                        tempData[a * 2 + 1] = (byte) (coorTbl[getDeviceMaxButton()*(1+k)*2+getDeviceMaxButton()+a] >> 8);
                    }
                    if (tranSetComboTbl((byte) k, (byte) (getDeviceMaxButton() * 2), (byte) 1, tempData)) {
                        if(bIsSave) {
                            if (tranSet_Value(Transfer_Con.HID_CMD_SAVE_COMBO_PTTBL, 10, k)) {
                                if (BuildConfig.DEBUG)
                                    Log.d(TAG, "Save Combo key success");
                            }
                        }
                    }
                }
        }
    }


    public void getCoordinateFromDevice_KB()
    {
        byte length = 20;
        int retry=0;
        byte []tempData = new byte[Transfer_Con.HID_PACKET_SIZE];
        if(getbIsUsbConnect())
        {
            for(int a=0;a<Transfer_Con.MAX_COORDINATE*2;)
            {
                for(retry=0;retry<5;retry++) {
                    if (tranGet_Value(Transfer_Con.HID_CMD_GET_POINT_TBL, a, length * 2, tempData)) {
                        for (int k = 0; k < length; k++) {
                            coorTbl[a + k] = (tempData[k * 2] & 0xff) + ((tempData[k * 2 + 1] & 0xff) << 8);
                        }
                        a += length;
                        if ((a + length) > Transfer_Con.MAX_COORDINATE * 2)
                            length = (byte) (Transfer_Con.MAX_COORDINATE * 2 - a);
                        break;
                    }
                }
                if(retry>=5)
                {
                    Toast.makeText(getApplicationContext(),"getCoordinateFromDevice_KB USB ERROR",Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        }
    }

    public void getCoordinateFromDevice_GP()
    {
        byte []tempData = new byte[Transfer_Con.HID_PACKET_SIZE];
        if(get_CurPtTbl((byte)(getDeviceMaxButton()*2),(byte)0,tempData))
        {
            short a;
            for (a = 0; a < getDeviceMaxButton(); a++) {
                coorTbl[a] = (int) (tempData[2 * a + 1] & 0xff) << 8;
                coorTbl[a] += (int) (tempData[2 * a] & 0xff);
            }
            if (get_CurPtTbl((byte) (getDeviceMaxButton() * 2), (byte) 1, tempData)) {
                for (a = 0; a < getDeviceMaxButton(); a++) {
                    coorTbl[getDeviceMaxButton() + a] = (int) (tempData[2 * a + 1] & 0xff) << 8;
                    coorTbl[getDeviceMaxButton() + a] += (int) (tempData[2 * a] & 0xff);
                    //str+=String.valueOf(ptTbl[mGlobalVar.getDeviceMaxButton()+a])+", ";
                }
            }
        }

        for (int k = 0; k < 4; k++) {
            if (tranGetComboTbl((byte) k, (byte) (getDeviceMaxButton() * 2), (byte) 0, tempData)) {
                int a;
                for (a = 0; a < getDeviceMaxButton(); a++) {
                    coorTbl[getDeviceMaxButton()*(1+ k)*2 + a] = (int) (tempData[2 * a + 1] & 0xff) << 8;
                    coorTbl[getDeviceMaxButton()*(1+ k)*2 + a] += (int) (tempData[2 * a] & 0xff);
                }

                if (tranGetComboTbl((byte) k, (byte) (getDeviceMaxButton() * 2), (byte) 1, tempData)) {
                    for (a = 0; a < getDeviceMaxButton(); a++) {
                        coorTbl[getDeviceMaxButton()*(1+ k)*2 + getDeviceMaxButton() +a] = (int) (tempData[2 * a + 1] & 0xff) << 8;
                        coorTbl[getDeviceMaxButton()*(1+ k)*2 + getDeviceMaxButton() +a] += (int) (tempData[2 * a] & 0xff);
                    }
                }
            }
        }
    }

    public void sendAttributeToDevice_KB()
    {
        byte length = 40;
        int retry =0;
        byte []tempData = new byte[Transfer_Con.HID_PACKET_SIZE];
        if(getbIsUsbConnect())
        {
            for(int i=0;i<5;i++)
            {
                for(int a=0;a<Transfer_Con.MAX_COORDINATE;)
                {
                    //Log.d(TAG,"--------------------------------"+String.valueOf(a));
                    if(i==0)
                    {
                        for (int k = 0; k < length; k++) {
                            tempData[k] = (byte) (attrTbl[a + k][i] & 0xff);
                        }
                    }
                    else if(i==1)
                    {
                        for (int k = 0; k < length; k++) {
                            tempData[k] = (byte) ((attrTbl[a + k][1] & 0xff));
                        }
                    }
                    else if(i==2)
                    {
                        for (int k = 0; k < length; k++) {
                            tempData[k] = (byte) ((attrTbl[a + k][1] & 0xff00)>>8);
                        }
                    }
                    else if(i==3)
                    {
                        for (int k = 0; k < length; k++) {
                            tempData[k] = (byte) ((attrTbl[a + k][2] & 0xff));
                        }
                    }
                    else //if(i==4)
                    {
                        for (int k = 0; k < length; k++) {
                            tempData[k] = (byte) ((attrTbl[a + k][2] & 0xff00)>>8);
                        }
                    }
                    for(retry=0;retry<5;retry++) {
                        if (tranSet_MultiValue(Transfer_Con.HID_CMD_SET_AEV_TBL, (i << 16) | a, length, tempData)) {
                            a += length;
                            if ((a + length) > Transfer_Con.MAX_COORDINATE * 2)
                                length = (byte) (Transfer_Con.MAX_COORDINATE * 2 - a);
                            break;
                        }
                    }
                    if(retry>=5)
                    {
                        Toast.makeText(getApplicationContext(),"sendAttributeToDevice_KB USB ERROR",Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
            }
        }
    }

    public void sendAttributeToDevice_GP(boolean bIsSave)
    {
        byte []tempData = new byte[Transfer_Con.HID_PACKET_SIZE];
        if(getbIsUsbConnect())
        {

            for (int a = 0; a < 3; a++) {
                String str ="";
                for (int k = 0; k < getDeviceMaxButton(); k++) {
                    tempData[k] = (byte)attrTbl[k][a];
                    str+=String.valueOf(tempData[k] )+", ";
                }
                for(int retry=0;retry<5;retry++) {
                    if (tranSet_MultiValue(Transfer_Con.HID_CMD_SET_KEY_ATTR, a, getDeviceMaxButton(), tempData)) {
                        if (BuildConfig.DEBUG)
                            Log.d(TAG, "attr write back ok: ");
                        break;
                    }
                }
            }
            if(bIsSave) {
                for (int retry = 0; retry < 5; retry++) {
                    if (tranSet_Value(Transfer_Con.HID_CMD_SAVE_DATA, 0, 0)) {
                        if (BuildConfig.DEBUG)
                            Log.d(TAG, "attr write save ok. ");
                        break;
                    }
                }
            }
        }
    }


    public void getAttributeFromDevice_KB()
    {
        byte length = 40;
        int retry=0;
        byte []tempData = new byte[Transfer_Con.HID_PACKET_SIZE];
        if(getbIsUsbConnect())
        {
            for(int i=0;i<5;i++)
            {
                for(int a=0;a<Transfer_Con.MAX_COORDINATE;)
                {
                    for(retry=0;retry<5;retry++) {
                        if (tranGet_Value(Transfer_Con.HID_CMD_GET_AEV_TBL, (i << 16) | a, length, tempData)) {
                            if (i == 0) {
                                for (int k = 0; k < length; k++) {
                                    attrTbl[a + k][i] = tempData[k] & 0xff;
                                }
                            } else if (i == 1) {
                                for (int k = 0; k < length; k++) {
                                    attrTbl[a + k][1] = (attrTbl[a + k][1] & 0xff00) + (tempData[k] & 0xff);
                                }
                            } else if (i == 2) {
                                for (int k = 0; k < length; k++) {
                                    attrTbl[a + k][1] = (attrTbl[a + k][1] & 0x00ff) + ((tempData[k] & 0xff) << 8);
                                }
                            } else if (i == 3) {
                                for (int k = 0; k < length; k++) {
                                    attrTbl[a + k][2] = (attrTbl[a + k][2] & 0xff00) + (tempData[k] & 0xff);
                                }
                            } else //if(i==4)
                            {
                                for (int k = 0; k < length; k++) {
                                    attrTbl[a + k][2] = (attrTbl[a + k][2] & 0x00ff) + ((tempData[k] & 0xff) << 8);
                                }
                            }
                            a += length;
                            if ((a + length) > Transfer_Con.MAX_COORDINATE * 2)
                                length = (byte) (Transfer_Con.MAX_COORDINATE * 2 - a);
                            break;
                        }
                    }
                    if(retry>=5)
                    {
                        Toast.makeText(getApplicationContext(),"getAttributeFromDevice_KB USB ERROR",Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
            }
        }
    }

    public void getAttributeFromDevice_GP()
    {
        byte []tempData = new byte[Transfer_Con.HID_PACKET_SIZE];
        if(getbIsUsbConnect())
        {
            for (int a = 0; a < 3; a++) {
                for(int retry=0;retry<5;retry++) {
                    if (tranGet_Value(Transfer_Con.HID_CMD_GET_KEY_ATTR, a, 0, tempData)) {
                        for (int k = 0; k < getDeviceMaxButton(); k++) {
                            attrTbl[k][a] = tempData[16 + k] & 0xff;
                        }
                        break;
                    }
                }
            }
        }
    }

    public boolean getIsSupportAttributeEventTable_KB()
    {
        if(getDeviceIndex()==Transfer_Con.DEVICE_MP1_INDEX)
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+6))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_BX1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_BX2_INDEX))
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+3))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_MR1_INDEX)||getDeviceIndex()==Transfer_Con.DEVICE_U41_INDEX)
        {
            return true;
        }
        return false;
    }

    public boolean getIsSupportEditMode_KB()
    {
        if(getDeviceIndex()==Transfer_Con.DEVICE_MP1_INDEX)
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+10))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_BX1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_BX2_INDEX))
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+10))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_MR1_INDEX)||getDeviceIndex()==Transfer_Con.DEVICE_U41_INDEX)
        {
            return true;
        }
        return false;
    }


    public boolean getIsSupportSwitchTVMode_KB()
    {
        if((getDeviceIndex()==Transfer_Con.DEVICE_BX1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_BX2_INDEX))
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+4))
            {
                return true;
            }
        }
        return false;
    }

    public boolean getIsSupportKeySplit_KB()
    {
        if(getDeviceIndex()==Transfer_Con.DEVICE_MP1_INDEX)
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+10))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_BX1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_BX2_INDEX))
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+9))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_MR1_INDEX)||getDeviceIndex()==Transfer_Con.DEVICE_U41_INDEX)
        {
            return true;
        }
        return false;
    }

    public boolean getIsSupportMultiShotHelp_KB()
    {
        if(getDeviceIndex()==Transfer_Con.DEVICE_MP1_INDEX)
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+14))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_BX1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_BX2_INDEX))
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+18))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_MR1_INDEX)||getDeviceIndex()==Transfer_Con.DEVICE_U41_INDEX)
        {
            return true;
        }
        return false;
    }

    public boolean getIsSupportShotHelpPosture_KB()
    {
        if(!Transfer_Con.ENABLE_SHOT_HELP_POSTURE)
            return false;

        if(getDeviceIndex()==Transfer_Con.DEVICE_MP1_INDEX)
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+15))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_BX1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_BX2_INDEX))
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+24))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_MR1_INDEX)||getDeviceIndex()==Transfer_Con.DEVICE_U41_INDEX)
        {
            return true;
        }
        return false;
    }

    public boolean getIsSupportShotHelpTrigger_KB()
    {
        if(!Transfer_Con.ENABLE_SHOT_HELP_POSTURE)
            return false;

        if(getDeviceIndex()==Transfer_Con.DEVICE_MP1_INDEX)
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+16))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_BX1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_BX2_INDEX))
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+29))
            {
                return true;
            }
        }
        else if(getDeviceIndex()==Transfer_Con.DEVICE_MR1_INDEX)
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+4))
            {
                return true;
            }
        }
        else if(getDeviceIndex()==Transfer_Con.DEVICE_U41_INDEX)
        {
            return true;
        }
        return false;
    }

    public boolean getIsSupportShotHelpTrigger1_KB()
    {
        if(!Transfer_Con.ENABLE_SHOT_HELP_POSTURE)
            return false;

        if(getDeviceIndex()==Transfer_Con.DEVICE_MP1_INDEX)
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+18))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_BX1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_BX2_INDEX))
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+32))
            {
                return true;
            }
        }
        else if(getDeviceIndex()==Transfer_Con.DEVICE_MR1_INDEX)
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+5))
            {
                return true;
            }
        }
        else if(getDeviceIndex()==Transfer_Con.DEVICE_U41_INDEX)
        {
            return true;
        }
        return false;
    }

    public boolean getIsSupportBagWithMouse_KB()
    {
        if(!Transfer_Con.ENABLE_SHOT_HELP_POSTURE)
            return false;

        if(getDeviceIndex()==Transfer_Con.DEVICE_MP1_INDEX)
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+19))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_BX1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_BX2_INDEX))
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+33))
            {
                return true;
            }
        }
        else if(getDeviceIndex()==Transfer_Con.DEVICE_MR1_INDEX)
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+6))
            {
                return true;
            }
        }
        else if(getDeviceIndex()==Transfer_Con.DEVICE_U41_INDEX)
        {
            return true;
        }
        return false;
    }

    public boolean getIsSupportVirtualMouse_KB()
    {
        if(!Transfer_Con.ENABLE_VIRTUAL_MOUSE)
            return false;

        if(getDeviceIndex()==Transfer_Con.DEVICE_MP1_INDEX)
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+16))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_BX1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_BX2_INDEX))
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+25))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_MR1_INDEX)||getDeviceIndex()==Transfer_Con.DEVICE_U41_INDEX)
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+3))
            {
                return true;
            }
        }
        return false;
    }

    public boolean getIsSupportMousePressSwitch_KB()
    {
        if(!Transfer_Con.ENABLE_VIRTUAL_MOUSE)
            return false;

        if(getDeviceIndex()==Transfer_Con.DEVICE_MP1_INDEX)
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+16))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_BX1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_BX2_INDEX))
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+36))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_MR1_INDEX)||getDeviceIndex()==Transfer_Con.DEVICE_U41_INDEX)
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+3))
            {
                return true;
            }
        }
        return false;
    }



    public boolean getIsSupportNewMacro_GP()
    {
        if((getDeviceIndex()==Transfer_Con.DEVICE_HN1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_HN2_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_GM1_INDEX))
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+25))
            {
                return true;
            }
        }
        return false;
    }

    public boolean getIsSupportMouseSideKeyAndHorSense()
    {
        if((getDeviceIndex()==Transfer_Con.DEVICE_BX1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_BX2_INDEX))
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+16))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_MP1_INDEX))
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+14))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_MR1_INDEX))
        {
            return true;
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_U41_INDEX))
        {
            return true;
        }

        return false;
    }

    public boolean getIsSupportShptHelpInsert_KB()
    {
        if(getDeviceIndex()==Transfer_Con.DEVICE_MP1_INDEX)
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+19))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_BX1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_BX2_INDEX))
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+37))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_MR1_INDEX)||getDeviceIndex()==Transfer_Con.DEVICE_U41_INDEX)
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+6))
            {
                return true;
            }
        }
        return false;
    }


    public boolean getIsSupportShptHelpInsertDefine_KB()
    {
        if(getDeviceIndex()==Transfer_Con.DEVICE_MP1_INDEX)
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+19))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_BX1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_BX2_INDEX))
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+38))
            {
                return true;
            }
        }
        else if((getDeviceIndex()==Transfer_Con.DEVICE_MR1_INDEX)||getDeviceIndex()==Transfer_Con.DEVICE_U41_INDEX)
        {
            if(get_IntVersion()>=((1<<24)+(1<<8)+6))
            {
                return true;
            }
        }
        return false;
    }

    public int getDirectCenterIndex()
    {
        return  0x90;
           /*
        if(getIsSupportMouseSideKeyAndHorSense())
        {
            return 0x90;
        }
        else
        {
            return 0x8D;
        }

            */
    }

    public void sendAttributeEventTableToDevice_KB()
    {
        int length = 40;
        int retry=0;
        byte []tempData = new byte[Transfer_Con.HID_PACKET_SIZE];
        if(!getIsSupportAttributeEventTable_KB())
            return;
        if(getbIsUsbConnect())
        {
            for (int a = 0; a < Transfer_Con.MAX_AUTOEVENT_LENGTH_KB; a += length) {
                if(a+length>Transfer_Con.MAX_AUTOEVENT_LENGTH_KB)
                {
                    length = (Transfer_Con.MAX_AUTOEVENT_LENGTH_KB -a) ;
                }

                for(int k=0;k<length;k++)
                {
                    tempData[k] = (byte)attrEvTbl[a+k];
                }

                for(retry=0;retry<5;retry++)
                {
                    if (tranSet_MultiValue(Transfer_Con.HID_CMD_SET_KEY_ATTR, a, (a + length >= Transfer_Con.MAX_AUTOEVENT_LENGTH_KB) ? (Transfer_Con.MAX_AUTOEVENT_LENGTH_KB - a) : length, tempData)) {
                        break;
                    }
                }
                if(retry>=5)
                {
                    Toast.makeText(getApplicationContext(),"sendAttributeEventTableToDevice_KB USB ERROR",Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        }
    }

    public void sendAttributeEventTableToDevice_GP(boolean bIsSave)
    {
        byte []tempData = new byte[Transfer_Con.HID_PACKET_SIZE];
        String str="";
        for (int a = 0; a < Transfer_Con.MAX_AUTOEVENT_LENGTH; a += 30) {

            for(int k=0;k<30;k++)
            {
                tempData[k] = (byte)attrEvTbl[a+k];
                str+=String.valueOf(tempData[k]&0xff)+", ";
            }
            for(int retry=0;retry<5;retry++) {
                if (tranSet_MultiValue(Transfer_Con.HID_CMD_SET_AEV_TBL, a, (a + 30 >= Transfer_Con.MAX_AUTOEVENT_LENGTH) ? (Transfer_Con.MAX_AUTOEVENT_LENGTH - a) : 30, tempData)) {
                    break;
                }
            }
        }
        if(bIsSave) {
            if (tranSet_Value(Transfer_Con.HID_CMD_SAVE_DATA, 0, 0)) {
                Log.d(TAG, "Attr ev save ok: ");
            }
        }
    }

    public void getAttributeEventTableFromDevice_KB()
    {
        byte length = 40;
        byte []tempData = new byte[Transfer_Con.HID_PACKET_SIZE];
        if(!getIsSupportAttributeEventTable_KB())
            return;
        for (int a = 0; a < Transfer_Con.MAX_AUTOEVENT_LENGTH_KB; a += length)
        {
            if(a+length>Transfer_Con.MAX_AUTOEVENT_LENGTH_KB)
            {
                length = (byte)(Transfer_Con.MAX_AUTOEVENT_LENGTH_KB -a) ;
            }
            for(int retry=0;retry<5;retry++) {
                if (tranGet_Value(Transfer_Con.HID_CMD_GET_KEY_ATTR, a, (a + length >= Transfer_Con.MAX_AUTOEVENT_LENGTH_KB) ? (Transfer_Con.MAX_AUTOEVENT_LENGTH_KB - a) : length, tempData)) {
                    //System.arraycopy(g_u8DataBuf, 16, mGlobalVar.attrEvTbl, a, (a + 30 >= Transfer_Con.MAX_AUTOEVENT_LENGTH) ? (Transfer_Con.MAX_AUTOEVENT_LENGTH - a) : 30);
                    for (int k = 0; k < length; k++) {
                        attrEvTbl[a + k] = tempData[16 + k] & 0xff;
                    }
                    break;
                }
            }
        }
    }

    public void getAttributeEventTableFromDevice_GP()
    {
        byte []tempData = new byte[Transfer_Con.HID_PACKET_SIZE];
        for (int a = 0; a < Transfer_Con.MAX_AUTOEVENT_LENGTH; a += 30) {
            for(int retry=0;retry<5;retry++) {
                if (tranGet_Value(Transfer_Con.HID_CMD_GET_AEV_TBL, a, (a + 30 >= Transfer_Con.MAX_AUTOEVENT_LENGTH) ? (Transfer_Con.MAX_AUTOEVENT_LENGTH - a) : 30, tempData)) {
                    //System.arraycopy(g_u8DataBuf, 16, mGlobalVar.attrEvTbl, a, (a + 30 >= Transfer_Con.MAX_AUTOEVENT_LENGTH) ? (Transfer_Con.MAX_AUTOEVENT_LENGTH - a) : 30);
                    for (int k = 0; k < 30; k++) {
                        attrEvTbl[a + k] = tempData[16 + k] & 0xff;
                    }
                    break;
                }
            }
        }
    }

    public void sendLocalSettingToDevice()
    {
        if(getIsSupportMultiShotHelp_KB()) {
            tranSet_Value_retry(Transfer_Con.HID_CMD_SET_ROCKER_RANGE, getRockerMoveRange_KB(), 0,3);
            if(bIsForceViewOut)
                tranSet_Value_retry(Transfer_Con.HID_CMD_SET_HW_SETTING,  0x08, 0x08,3);
            else
                tranSet_Value_retry(Transfer_Con.HID_CMD_SET_HW_SETTING,  0x00, 0x08,3);
        }
    }

    public boolean getIsCurDeviceGamePad()
    {
        return ((getDeviceIndex()==Transfer_Con.DEVICE_HN1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_GM1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_HN2_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_GM2_INDEX));
    }
    public boolean getIsLastConDeviceGamePad()
    {
        return((getLastConnectDeviceIndex()==Transfer_Con.DEVICE_HN1_INDEX)||(getLastConnectDeviceIndex()==Transfer_Con.DEVICE_GM1_INDEX)||(getLastConnectDeviceIndex()==Transfer_Con.DEVICE_HN2_INDEX)||(getLastConnectDeviceIndex()==Transfer_Con.DEVICE_GM2_INDEX));
    }

    public boolean getIsCurDeviceKB()
    {
        return((getDeviceIndex()==Transfer_Con.DEVICE_BX1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_BX2_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_MP1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_MR1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_U41_INDEX));
    }
    public boolean getIsLastConDeviceKB()
    {
        return((getLastConnectDeviceIndex()==Transfer_Con.DEVICE_BX1_INDEX)||(getLastConnectDeviceIndex()==Transfer_Con.DEVICE_BX2_INDEX)||(getLastConnectDeviceIndex()==Transfer_Con.DEVICE_MP1_INDEX)||(getLastConnectDeviceIndex()==Transfer_Con.DEVICE_MR1_INDEX)||(getLastConnectDeviceIndex()==Transfer_Con.DEVICE_U41_INDEX));
    }

    public boolean getIsCurDeviceKBBox()
    {
        return((getDeviceIndex()==Transfer_Con.DEVICE_BX1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_BX2_INDEX));
    }
    public boolean getIsLastConDeviceKBBox()
    {
        return((getLastConnectDeviceIndex()==Transfer_Con.DEVICE_BX1_INDEX)||(getLastConnectDeviceIndex()==Transfer_Con.DEVICE_BX2_INDEX));
    }

    public boolean getIsCurDeviceKB_Monitor()
    {
        return((getDeviceIndex()==Transfer_Con.DEVICE_MP1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_MR1_INDEX)||(getDeviceIndex()==Transfer_Con.DEVICE_U41_INDEX));
    }
    public boolean getIsLastConDeviceKB_Monitor()
    {
        return((getLastConnectDeviceIndex()==Transfer_Con.DEVICE_MP1_INDEX)||(getLastConnectDeviceIndex()==Transfer_Con.DEVICE_MR1_INDEX)||(getLastConnectDeviceIndex()==Transfer_Con.DEVICE_U41_INDEX));
    }

}
